Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2 ngOnChanges在输入快速变化时不触发_Angular_Angular2 Template_Ngonchanges - Fatal编程技术网

Angular 角度2 ngOnChanges在输入快速变化时不触发

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 {

我使用Angular 2@Input属性将所需的数值传递给子组件,如下所示

父组件:

@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()