admin管理员组文章数量:1293149
I am working on this angular2 app in which I am accepting two inputs in one ponent.
I have used ngOnChanges to detect changes on these input values.
@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
}
However, I can only detect change in first variable. the second variable is not getting updated when its value changes in parent.
any inputs?
I am working on this angular2 app in which I am accepting two inputs in one ponent.
I have used ngOnChanges to detect changes on these input values.
@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
}
However, I can only detect change in first variable. the second variable is not getting updated when its value changes in parent.
any inputs?
Share asked Aug 23, 2016 at 10:46 Bhushan GadekarBhushan Gadekar 13.8k21 gold badges88 silver badges131 bronze badges 1- Can you reproduce this issue in Plunker? – Yaron Schwimmer Commented Aug 23, 2016 at 10:52
2 Answers
Reset to default 6Depending on the change from the parent ponent, ngOnChanges might be triggered separately for each Input changed. You need to check which Input has changed first.
ngOnChanges(changes: SimpleChange}) {
if(changes['selectedGame'] !== undefined)
this.selectedGame=changes['selectedGame'].currentValue;
if(changes['games'] !== undefined)
this.games=changes['games'].currentValue;
}
Here's the working plunk https://plnkr.co/edit/n6X21VHXw1xlA8XCPoQu
You supposed to use SimpleChanges
instaed of SimpleChange
, it will give you all list of input parameters.
ngOnChanges(changes: SimpleChanges) {
for (let propName in changes) {
let change = changes[propName];
let curVal = JSON.stringify(change.currentValue);
let prevVal = JSON.stringify(change.previousValue);
console.log(curVal);
console.log(prevVal);
}
}
本文标签: javascriptangular2 ngOnChanges not working for multiple inputsStack Overflow
版权声明:本文标题:javascript - angular2 ngOnChanges not working for multiple inputs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741573602a2386153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论