On Github dpsthree / ng2talk
@Component({
selector: 'my-app'
})
@View({
template: '<h1>Hello {{ name }}</h1>'
})
class MyAppComponent {
name: string;
constructor() {
this.name = 'Alice';
}
}
This is an NG2 component. Everything that you see in an Angular2 application is associated with a component.
And almost everything in Angular 2 is a component~.
By identifying an application level component and referring to child components in its template we build
applications out of a component tree.
@Component({
selector: 'child-cmp',
properties: ['providedByParent'],
})
@View({
templateUrl: 'child_cmp.html'
})
class ChildCmp {
providedByParent: string;
//...
}
<div>
Hello, the following is data from the parent component: {{ providedByParent }}
</div>
This is an example of a component with a property. You declare that the component has the property and
any bindings made using that property will be updated as its value changes in the parent scope.
@Component({
selector: 'child-cmp',
events: ['childDataEmitter']
})
@View({
templateUrl: 'child_cmp.html'
})
class ChildCmp {
childDataEmitter: EventEmitter;
//...
}
<child-cmp (childDataEmitter)="childEventWasCalled()">
childEventWasCalled is a function on the parent
</child-cmp>
This is an example of a component that emits an event and its declaration in the HTML. There are all
kinds of tricks and syntax sugar for this system that we may get into later if there is time.
@Component({
selector: 'my-app',
bindings: 'MyThing'
})
@View({
template: '<h1>Hello {{ name }}</h1>
<child-cmp></child-cmp>',
directives: ['childCmp']
})
class MyAppComponent {
name: string;
constructor(thing1: MyThing) {
this.name = 'Alice';
}
}
@Component({
selector: 'child-cmp'
//did not specify binding because I do not want to override the one above
})
@View({
template: '<div>placeholder</div>'
})
class ChildCmp {
constructor(thing1: MyThing){
//thing1 is the same instance as above
}
}