gpt4 book ai didi

javascript - angular2 ngOnChanges 不适用于多个输入

转载 作者:搜寻专家 更新时间:2023-10-30 21:30:37 26 4
gpt4 key购买 nike

我正在开发这个 angular2 应用程序,其中我在一个组件中接受两个输入。

我使用 ngOnChanges 来检测这些输入值的变化。

@Input() games: any;
@Input() selectedGame:any;

ngOnChanges(changes: {[propName: string]: SimpleChange}) {
this.selectedGame=changes['selectedGame'].currentValue; //this works
this.games=changes['games'].currentValue; //this doesn't work
}

但是,我只能检测第一个变量的变化。当第二个变量的值在父级中发生变化时,第二个变量没有得到更新。

有什么输入吗?

最佳答案

根据父组件的更改,可能会为每个输入更改单独触发 ngOnChanges。您需要先检查哪个 Input 发生了变化。

ngOnChanges(changes: SimpleChange}) {   
if(changes['selectedGame'] !== undefined)
this.selectedGame=changes['selectedGame'].currentValue;

if(changes['games'] !== undefined)
this.games=changes['games'].currentValue;
}

这是工作原理 https://plnkr.co/edit/n6X21VHXw1xlA8XCPoQu

关于javascript - angular2 ngOnChanges 不适用于多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com