5

I am working on this angular2 app in which I am accepting two inputs in one component.

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?

1
  • Can you reproduce this issue in Plunker? Commented Aug 23, 2016 at 10:52

2 Answers 2

6

Depending on the change from the parent component, 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

Sign up to request clarification or add additional context in comments.

Comments

2

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);
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.