2
我正在使用Angular 2 @Input属性将所需的数值传递给子组件,如下所示。Angular 2 ngOnChanges不会触发快速更改的输入
父组件:
@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生命周期钩被触发仅一次,而不是3次,显示出从0变化到输入值3.但是我需要在每次更改值时触发它(0 - > 1,1 - > 2,2 - > 3等)。有没有办法如何做到这一点?
感谢。