Created
November 19, 2016 03:29
-
-
Save aitorjs/faea3584189e279591ac69f50aa98b03 to your computer and use it in GitHub Desktop.
pass data angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { bootstrap } from 'angular2/platform/browser'; | |
import { Component, EventEmitter } from 'angular2/core'; | |
import { NgFor } from 'angular2/common'; | |
@Component({ | |
selector: 'sub-component', | |
inputs: ['items'], | |
outputs: ['onItemSelected'], | |
directives: [NgFor], | |
template: ` | |
<div class="item" *ngFor="#item of items; #i = index"> | |
<span>{{ item }}</span> | |
<button type="button" (click)="select(i)">Select</button> | |
</div> | |
` | |
}) | |
class SubComponent { | |
onItemSelected: EventEmitter<string>; | |
items: string[]; | |
constructor() { | |
this.onItemSelected = new EventEmitter(); | |
} | |
select(i) { | |
this.onItemSelected.emit(this.items[i]); | |
} | |
} | |
@Component({ | |
selector: 'app', | |
directives: [SubComponent], | |
template: ` | |
<div> | |
<sub-component [items]="items" (onItemSelected)="itemSelected($event)"> | |
</sub-component> | |
</div> | |
` | |
}) | |
class App { | |
items: string[]; | |
constructor() { | |
this.items = ['item1', 'item2', 'item3']; | |
} | |
itemSelected(item: string): void { | |
console.log('Selected item:', item); | |
} | |
} | |
bootstrap(App); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment