<!DOCTYPE html>
<html>
<head>
<title>Angular 2 App</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="https://npmcdn.com/core-js/client/shim.min.js"></script>
<script src="https://npmcdn.com/zone.js@0.6.12?main=browser"></script>
<script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
<script src="https://npmcdn.com/systemjs@0.19.27/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<sample-app>Loading...</sample-app>
</body>
</html>
# Angular 2 Sample Starter
A starting point for an Angular 2 sample running on Plunker.
Uses:
- Angular 2 RC1
- TypeScript
- SystemJS
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
/**
* PLUNKER VERSION (based on systemjs.config.js in angular.io)
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function(global) {
var ngVer = '@2.0.0-rc.1'; // lock in the angular package version; do not let it float to current!
//map tells the System loader where to look for things
var map = {
'app': 'app',
'@angular': 'https://npmcdn.com/@angular', // sufficient if we didn't pin the version
'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api', // get latest
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6',
'ts': 'https://npmcdn.com/plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'https://npmcdn.com/typescript@1.8.10/lib/typescript.js',
};
//packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.ts', defaultExtension: 'ts' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
};
var ngPackageNames = [
'common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'router-deprecated',
'upgrade',
];
// Add map entries for each angular package
// only because we're pinning the version with `ngVer`.
ngPackageNames.forEach(function(pkgName) {
map['@angular/'+pkgName] = 'https://npmcdn.com/@angular/' + pkgName + ngVer;
});
// Add package entries for angular packages
ngPackageNames.forEach(function(pkgName) {
// Bundled (~40 requests):
packages['@angular/'+pkgName] = { main: pkgName + '.umd.js', defaultExtension: 'js' };
// Individual files (~300 requests):
//packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
});
var config = {
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
map: map,
packages: packages
}
System.config(config);
})(this);
import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
bootstrap(AppComponent);
import {Component, Input, Output, ViewChild,
DivComponent, ComponentFactory, ComponentResolver,
Injectable, ComponentRef, Directive, ViewContainerRef, EventEmitter} from "@angular/core";
@Component({
selector: 'child-comp',
template: `
<input type="text" [(ngModel)]="value" (change)="change($event)" (keyup)="change($event)"/>
`
})
export class StaticChildComp implements OnChanges {
@Input() public value: any;
@Output() public valueChange:EventEmitter = new EventEmitter();
change() {
this.valueChange.emit(this.value);
}
ngOnChanges(changes: {}): any {
console.log("From Static Child:" + JSON.stringify(changes));
}
}
@Component({
selector: 'dynamic-child-comp',
template: `
<input type="text" [(ngModel)]="value" (change)="change($event)"/>
`,
inputs: ["value"],
outpus: ["valueChange"]
})
export class DynamicChildComp extends StaticChildComp {
ngOnChanges(changes: {}): any {
console.log("From Dynamic Child:" + JSON.stringify(changes));
}
ngDoCheck() {
console.log("Change Detection called");
}
}
@Component({
selector: 'parent-comp',
template: `
<span>What Parent can see: {{value.name}}</span>
<br/>
<label>Static Child</label><child-comp [(value)]="value.name" (valueChange)="onChildValueChange($event)"></child-comp>
<br/>
<label>Dynamic Child</label><div #child></div>
<br/>
<label>Dynamic Child 2</label><div #child2></div>
`,
directives: [StaticChildComp, DynamicChildComp]
})
export class ParentComp {
@Input() value: any;
constructor(protected cr: ComponentResolver) { }
@ViewChild("child",{read: ViewContainerRef}) myChild;
@ViewChild("child2",{read: ViewContainerRef}) myChild2;
ngOnInit() {
return this.cr.resolveComponent(DynamicChildComp)
.then((cf: ComponentFactory<any>) => {
///comp1 -added to #child control DOM
let ref = this.myChild.createComponent(cf);
ref.instance.value = this.value.name;
ref.instance.valueChange.subscribe( (value: any) => {
this.onChildValueChange(value);
});
///comp2 -added to #child control DOM
let ref2 = this.myChild2.createComponent(cf);
ref2.instance.value = this.value.name;
ref2.instance.valueChange.subscribe( (value: any) => {
this.onChildValueChange(value);
});
});
}
ngOnChanges (changes: {
[key: string]: SimpleChange;
}) {
console.log("Changes" + JSON.stringify(changes));
}
onChildValueChange (newValue) {
console.log(JSON.stringify(newValue));
this.value.name = newValue;
}
}
@Component({
selector: 'sample-app',
template: `
<label>Application</label><input type="text" [(ngModel)]="model.name"/>
<br/>
<parent-comp [value]="model"></parent-comp>
`,
directives: [ParentComp]
})
export class AppComponent {
@Output() model = {"name" : "Divyakumar"};
}