Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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 角度4:如何对@input变量更改作出反应_Angular - Fatal编程技术网

Angular 角度4:如何对@input变量更改作出反应

Angular 角度4:如何对@input变量更改作出反应,angular,Angular,我有一个名为modelforchildcomponent的输入变量。 @输入()模型:任意 我有一个父控件parentControl,它将更新的输入变量发送到childComponent 如何编写childComponent以响应模型更改? 我希望在模型更改时进行childComponent调用 想法?考虑使用生命周期 @Input() model: any; ngOnChanges(model: any) { this.doSomething(model); } 毫不奇怪,这是在文档

我有一个名为modelforchildcomponent的输入变量。 @输入()模型:任意

我有一个父控件parentControl,它将更新的输入变量发送到childComponent

如何编写childComponent以响应模型更改? 我希望在模型更改时进行childComponent调用

想法?

考虑使用生命周期

@Input() model: any;
ngOnChanges(model: any) {
    this.doSomething(model);
}

毫不奇怪,这是在文档中:更准确地说,如果有多个输入参数,我会为每个输入变量设置一个ngOnChanges吗?是的,你需要这样做,angular2中没有我们在angularjs@reza不,你没有,单个
ngOnChanges
方法的参数实际上是
SimpleChanges
,它包含对任何输入的更改。你可以在链接的文档中看到这一点,但不知道作者为什么会这样说。@reza是的,你是对的!实际上,我提到了使用set_方法,在这种情况下,您需要单独的方法。但是ngOnchanges通常会在输入发生变化时给出任何信息。对不起,我没有正确阅读你的评论。这很接近,但实际上并不正确。我将给出一个布尔数据绑定的例子``@Input()yourData:boolean;ngOnChanges(changes:SimpleChanges){console.log('changes:',changes);//这将控制台掉它检测到的所有更改。console.log('changes.yourData:',changes.yourData);//这将控制台掉旧数据和新数据。}``这些更改。yourData将在控制台中这样输出。````changes.yourData:{currentValue:true,firstChange:false,previousValue:Undefined}``previousValue将在第二次更改时更新