问题
[Angular2 @ RC4] [angularfire2 @ 2.0.0-beta.2]
Inside my child component I cannot get changes['posX'].previousValue
to store anything.
Snippet from parent html:
//inside *ngfor loop
[posX]='coord.position_x | calculateX:395'
[posY]='coord.position_y | calculateY:380'
Snippet from Child component:
export class childComponent implements OnChanges {
@Input() posX: number; //updated via firebase database from parant
@Input() posY: number; //updated via firebase database from parant
ngOnChanges(changes: {[ propName: string]: SimpleChange}) {
console.log('Change detected: ', changes['posX'].currentValue);
console.log('Change detected: ', changes['posX'].previousValue);
}
Result from chrome console :
Change detected: posX current = 343 //changes every time
Change detected: posX previous = Object {} //same after updates
I found this plnkr of ngOnChange and was able to get it working in my app. Although I'm unable to get the PosX and PosY variables working the same way.
The Variables PosX and PosY are being updated via an observable from my firebase database every 15 seconds. Any idea why previousValue is not showing up?
回答1:
Best answer I could find was about OnChanges() not being able to detect changes to coord because it is a reference to the coord object and not the actual value.
Direct Quote:
The value of the hero property is the reference to the hero object. Angular doesn't care that the hero's own name property changed. The hero object reference didn't change so, from Angular's perspective, there is no change to report!
Link: https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#onchanges
来源:https://stackoverflow.com/questions/38317945/ngonchange-not-storing-previousvalue-property