Angular 角度2 ngOnChanges在输入快速变化时不触发
我使用Angular 2@Input属性将所需的数值传递给子组件,如下所示 父组件:Angular 角度2 ngOnChanges在输入快速变化时不触发,angular,angular2-template,ngonchanges,Angular,Angular2 Template,Ngonchanges,我使用Angular 2@Input属性将所需的数值传递给子组件,如下所示 父组件: @Component({ selector: 'test-parent', template: '<button (click)="raiseCounter()">Click me!</button><test-child [value]="counter"></test-child>' }) export class ParentComponent {
@Component({
selector: 'test-parent',
template: '<button (click)="raiseCounter()">Click me!</button><test-child [value]="counter"></test-child>'
})
export class ParentComponent {
public counter: number = 0;
raiseCouner() {
this.counter += 1;
this.counter += 1;
this.counter += 1;
}
}
@Component({
selector: 'test-child'
});
export class ChildComponent implments OnChanges {
@Input() value: number = 0;
ngOnChanges() {
if (this.value) {
this.doSomeWork();
}
}
doSomeWork() {
console.log(this.value);
}
}
在这个场景中,OnChanges生命周期钩子只触发一次而不是三次,这表明输入值从0更改为3。但是,我需要在每次值更改时触发它(0->1、1->2、2->3等)。有没有办法做到这一点
谢谢。这是意料之中的行为。
Angular2在第三个
+=1
事件处理程序完成后运行更改检测 当更改检测更新
@Input()
绑定时,将调用ngOnChanges()