saveOccuraces(currentFrequency: Frequencies) {
var freq = new Frequencies();
freq = currentFrequency;
// freq.occurance = currentFrequency.occurance; // <--Problem is here
this.selectedFrequencies.push(freq);
}
<md-input-container>
<input type="number" [(ngModel)]="currentFrequency.occurance" mdInput placeholder="Every" name="occurance">
</md-input-container>
<button md-icon-button (click)="saveOccuraces(currentFrequency)">
<md-icon>add</md-icon>
</button>
在上面的代码中,如果我按原样执行,所有出现值都会更新为新的,如果我发表评论freq=currentFrequency;
行并取消注释freq.occurance = currentFrequency.occurance;
,那么它的工作正常。
是否可以选择仅使用值推送对象(withou
)答案 0 :(得分:1)
您可以在Frequencies
中创建一个构造函数,如下所示
Frequencies(frequencies:Frequencies){
this.occurance = frequencies.occurance;
}
像这样更改代码
saveOccuraces(currentFrequency:Frequencies){
var freq= new Frequencies(currentFrequency);
this.selectedFrequencies.push(freq);
}
使用Object.Assign()的替代方法
saveOccuraces(currentFrequency:Frequencies){
var freq= Object.assign(new Frequencies(),currentFrequency);
this.selectedFrequencies.push(freq);
}