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" | |
| }, | |
| //packages defines our app package | |
| packages: { | |
| app: { | |
| main: './main.ts', | |
| defaultExtension: 'ts' | |
| } | |
| } | |
| }); |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>angular2 playground</title> | |
| <link rel="stylesheet" href="style.css" /> | |
| <script src="https://code.angularjs.org/2.0.0-beta.16/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 src="https://code.angularjs.org/2.0.0-beta.16/Rx.js"></script> | |
| <script src="https://code.angularjs.org/2.0.0-beta.16/angular2.dev.js"></script> | |
| <script src="https://code.angularjs.org/2.0.0-beta.16/http.dev.js"></script> | |
| <script> | |
| System.import('app') | |
| .catch(console.error.bind(console)); | |
| </script> | |
| </head> | |
| <body> | |
| <my-app> | |
| loading... | |
| </my-app> | |
| </body> | |
| </html> |
| //our root app component | |
| import {Component, Directive, Provider, forwardRef, ViewChild, Input, Output, EventEmitter} from 'angular2/core' | |
| import {NG_VALIDATORS} from 'angular2/common' | |
| @Component({ | |
| selector: 'my-thing', | |
| template: 'My Thing' | |
| }) | |
| class Foo { | |
| } | |
| @Component({ | |
| selector: 'my-app', | |
| template: ` | |
| <template [ngIf]="show"> | |
| <button type="button" (click)="onClickFirst(thing)">Click me</button> Result: {{ firstText }} | |
| </template> | |
| <br> | |
| <button type="button" (click)="onClickSecond(thing)">Click me</button> Result: {{ secondText }} | |
| <br> | |
| <my-thing #thing></my-thing> | |
| `, | |
| directives: [Foo] | |
| }) | |
| export class App { | |
| show = true; | |
| firstText = ''; | |
| secondText = ''; | |
| onClickFirst(thing: any) { | |
| this.firstText = thing ? 'A Thing' : 'undefined' | |
| } | |
| onClickSecond(thing: any) { | |
| this.secondText = thing ? 'A Thing' : 'undefined' | |
| } | |
| } |
| //main entry point | |
| import {bootstrap} from 'angular2/platform/browser'; | |
| import {App} from './app'; | |
| bootstrap(App, []) | |
| .catch(err => console.error(err)); |
| /* Styles go here */ |