<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-beta.17/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
/* Styles go here */
### Angular2 Starter Plunker - Typescript - RC.0
A simple plunker demonstrating Angular2 usage:
- Uses SystemJS + TypeScript to compile on the fly
- Includes binding, directives, http, pipes, and DI usage.
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
//map tells the System loader where to look for things
map: {
app: "./src",
'@angular': 'https://npmcdn.com/@angular',
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
'@angular/core': {
main: 'bundles/core.umd.js',
defaultExtension: 'js'
},
'@angular/compiler': {
main: 'bundles/compiler.umd.js',
defaultExtension: 'js'
},
'@angular/common': {
main: 'bundles/common.umd.js',
defaultExtension: 'js'
},
'@angular/platform-browser-dynamic': {
main: 'bundles/platform-browser-dynamic.umd.js',
defaultExtension: 'js'
},
'@angular/platform-browser': {
main: 'bundles/platform-browser.umd.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule);
//our root app component
import {
Component,Directive, ComponentMetadata, Input, ReflectiveInjector,
ViewContainerRef, Compiler,NgModule
} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
@Directive({
selector: '[componentOutlet]',
})
export class ComponentOutlet {
@Input('componentOutlet') private template: string;
@Input('componentOutletSelector') private selector: string;
@Input('componentOutletContext') private context: Object;
constructor(private vcRef: ViewContainerRef, private compiler: Compiler) { }
private _createDynamicComponent() {
this.context = this.context || {};
const metadata = new ComponentMetadata({
selector: this.selector,
template: this.template,
});
const cmpClass = class _ { };
cmpClass.prototype = this.context;
return Component(metadata)(cmpClass);
}
ngOnChanges() {
if (!this.template) return;
this.compiler.compileComponentAsync(this._createDynamicComponent())
.then(factory => {
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
this.vcRef.clear();
this.vcRef.createComponent(factory, 0, injector);
});
}
}
@Component({
selector: 'my-app',
template: `
<p>{{message}}</p>
<div *componentOutlet="html; context:self; selector:'my-dynamic-component'"></div>
`,
})
export class App {
message = 'static component';
self = this; // copy of context
html = `
<div>
<button (click)="self.setMessage('dynamic component')">Click</button>
</div>`;
setMessage(message: string) {
this.message = message;
}
}
@NgModule({
imports: [BrowserModule],
declarations: [App, ComponentOutlet],
bootstrap: [App]
})
export class AppModule {}