這是一個後續:ngOnChange not storing previousValue propertyangular2 SimpleChange沒有存儲`previousValue`
我使用ionic2
/angular2
與此組件:https://github.com/crisbeto/angular-svg-round-progressbar和使用ChromeDevTools以下標記
<ion-card class="timer"
*ngFor="let snapshot of timers | toJSON"
>
<ion-card-content>
<round-progress
[current]="snapshot.remaining"
[max]="snapshot.duration"
[rounded]="true"
[responsive]="true"
[duration]="18"
>
</round-progress>
</ion-card>
,我在SimpleChange.current
中看到以下內容:
/** Change detection callback. */
ngOnChanges(changes): void {
if (changes.current) {
this._animateChange(changes.current.previousValue, changes.current.currentValue);
} else {
this._setPath(this.current);
}
}
// changes.current.previousValue = {}
// changes.current.cureentValue = 123 // int
// changes.current.isFirstChange() = true
爲什麼我不存儲changes.current.previousValue
正確嗎?
的toJSON
管調用該方法,它返回一個匿名對象:
/**
* create a snapshot of the timer
*/
toJSON() : TimerAttributes {
const remaining = this.check(true);
console.log(this.id, this.duration, remaining)
return {
id: this.id,
label: this.label,
// asMilliseconds
duration: this.duration,
// asMilliseconds
remaining: remaining,
humanize: this.humanize(remaining),
// as Unixtime
expires: this.expires
}
}
我唯一的猜測是,我返回不同的對象snapshot
每個變化檢測迴路,所以我失去previousValue
。但如果是這樣,簡單的解決方法是什麼?
我的猜測是正確的。如果我使用Object.assign(snapshot,o.toJSON())',那麼'snapshot'引用* same *對象,'SimpleChange.previousValue'正確保存'snapshot.remaining'的前一個值。但是,那麼我有可能得到'檢查出錯後,表達已經改變'。解決辦法是什麼? – michael