angular2 SimpleChange未存储'previousValue'`
这是以下方面的后续行动: 我将angular2 SimpleChange未存储'previousValue'`,angular,Angular,这是以下方面的后续行动: 我将ionic2/angular2与此组件一起使用:以及以下标记 <ion-card class="timer" *ngFor="let snapshot of timers | toJSON" > <ion-card-content> <round-progress [current]="snapshot.remaining" [max]="snapshot.duration"
ionic2
/angular2
与此组件一起使用:以及以下标记
<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>
为什么我没有正确存储更改.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
}
}
我唯一的猜测是,在每个更改检测循环中,我为快照返回了不同的对象,因此我丢失了以前的值。但是如果是这样,那么什么是容易解决的问题呢?如果
@Input
变量的对象发生更改
/**
* create a snapshot of the timer, return as anonymous object
*/
toJSON() : TimerAttributes {
const remaining = this.check();
// 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
}
}
/**
* return the SAME object with updated attr value
*/
snap(): TimerAttributes {
Object.assign( this.snapshot, this.toJSON())
return this.snapshot;
}
如果我的toJSONPipe
调用o.toJSON()
我会得到一个带有属性值的匿名对象,并且SimpleChange
不会存储更改.current.previousValue
如果我的
toJSONPipe
调用o.snap()
我将获得具有更新属性值的相同属性o.snapshot
,并且SimpleChange
正确存储更改.current.previousValue
我的猜测是正确的。如果我使用Object.assign(snapshot,o.toJSON())
,那么snapshot
引用同一个对象并且SimpleChange.previousValue
正确地保存了快照的previousValue。剩余的。但是,在检查错误后,我有可能得到一个表达式已更改。解决办法是什么?
/**
* create a snapshot of the timer, return as anonymous object
*/
toJSON() : TimerAttributes {
const remaining = this.check();
// 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
}
}
/**
* return the SAME object with updated attr value
*/
snap(): TimerAttributes {
Object.assign( this.snapshot, this.toJSON())
return this.snapshot;
}