Qwe*_*tiy 10 typescript angular
changeAngular 2中的活动是什么?什么时候发送,我该如何使用它?
I. e.我通过以下代码订阅了(change)="update()"什么?
http://plnkr.co/edit/mfoToOSLU6IU2zr0A8OB?p=preview
import {Component, View, Input, Output, EventEmitter, OnChanges} from '@angular/core'
@Component({
selector: 'inner-component',
template: `
<label><input type="checkbox" [(ngModel)]="data.isSelected"> Selected</label>
`
})
export class InnerComponent {
data = { isSelected: false };
}
@Component({
selector: 'my-app',
template: `
<p><inner-component (change)="update()"></inner-component></p>
<p>The component was updated {{count}} times</p>
`,
directives: [InnerComponent]
})
export class AppComponent {
count = 0;
update() {
++this.count;
}
}
Run Code Online (Sandbox Code Playgroud)
PS:俄语同样的问题.
Qwe*_*tiy 15
这是事件冒泡:change发生input,然后是dom树的气泡,并在inner-component元素上处理.可以通过记录事件来检查:
http://plnkr.co/edit/J8pRg3ow41PAqdMteKwg?p=preview
@Component({
selector: 'my-app',
template: `
<p><inner-component (change)="update($event)"></inner-component></p>
<p>The component was updated {{count}} times</p>
`,
directives: [InnerComponent]
})
export class AppComponent {
count = 0;
update($event) {
console.log($event, $event.target, $event.currentTarget);
++this.count;
}
}
Run Code Online (Sandbox Code Playgroud)
该change事件会通知您输入字段中发生的更改.由于您的内部组件不是本机Angular组件,因此您必须自己指定事件发射器:
@Component({
selector: 'inner-component',
template: `
<label><input type="checkbox" (change)="inputChange.emit($event)" [(ngModel)]="data.isSelected"> Selected</label>
`
})
export class InnerComponent {
@Output('change') inputChange = new EventEmitter();
data = { isSelected: false };
}
Run Code Online (Sandbox Code Playgroud)
在您的AppComponent中,您现在正在接收事件:
@Component({
selector: 'my-app',
template: `
<p><inner-component (change)="update($event)"></inner-component></p>
<p>The component was updated {{count}} times</p>
`,
directives: [InnerComponent]
})
export class AppComponent {
count = 0;
update(event: any) {
++this.count;
console.log(event);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35731 次 |
| 最近记录: |