Skip to content

Instantly share code, notes, and snippets.

@michaelbromley
Created June 24, 2015 06:02
Show Gist options
  • Save michaelbromley/48947220f7a115a0486f to your computer and use it in GitHub Desktop.
Save michaelbromley/48947220f7a115a0486f to your computer and use it in GitHub Desktop.
Angular 2 Demo Code
/// <reference path="typings/angular2/angular2.d.ts" />
import {Component, Directive, View, Inject, coreDirectives, bootstrap} from 'angular2/angular2';
import {NameService} from 'nameService';
import {Logger} from 'logger';
@Component({
selector: 'my-app'
})
@View({
template: `
<div>
<input type="text" #input (keyup)="update(input.value)" logger>
<div *ng-for="#name of names">
<name-component name="name">Hello {{ name }}</name-component>
</div>
</div>
`,
directives: [coreDirectives, Logger]
})
class MyAppComponent {
private names;
constructor(@Inject(NameService) nameService) {
this.names = [nameService.getName()];
}
update(val: string) {
this.names = val.split(',');
}
}
bootstrap(MyAppComponent, [NameService]);
<html>
<head>
<title>Angular 2 Demo</title>
<script src="https://github.jspm.io/jmcriffey/[email protected]/traceur-runtime.js"></script>
<script src="https://jspm.io/[email protected]"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.26/angular2.dev.js"></script>
</head>
<body>
<!-- The app component created in app.ts -->
<my-app></my-app>
<script>System.import('app');</script>
</body>
</html>
/// <reference path="typings/angular2/angular2.d.ts" />
import {Directive} from 'angular2/angular2';
@Directive({
selector: '[logger]',
hostListeners: {
'change': 'logValue($event)'
}
})
export class Logger {
constructor() {
console.log('logger instantiated!');
}
logValue(event: Event) {
var target: HTMLInputElement = <HTMLInputElement>event.target;
console.log(target.tagName + ' value is: ' + target.value);
}
}
export class NameService {
private name;
constructor() {
this.name = "AngularJS Vienna";
}
getName() {
return this.name;
}
}
@michaelbromley
Copy link
Author

This is the demo code I used in a talk "Angular 2 - A First Look", given on 23/06/15 at the AngularJS Vienna meetup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment