1
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>

In the above code if I execute as it is, all occurrence values are updating with new one, if i comment freq=currentFrequency; line and un comment freq.occurance = currentFrequency.occurance; then it is working fine.

Is there any alternative to push objects with value only (withou

3

1 Answer 1

1

You can create a constructor in Frequencies like below

Frequencies(frequencies:Frequencies){
this.occurance = frequencies.occurance;
}

Change the code like this

saveOccuraces(currentFrequency:Frequencies){
        var freq= new Frequencies(currentFrequency);
        this.selectedFrequencies.push(freq);
    }

Alternative Way using Object.Assign()

saveOccuraces(currentFrequency:Frequencies){
            var freq= Object.assign(new Frequencies(),currentFrequency);
            this.selectedFrequencies.push(freq);
        }
Sign up to request clarification or add additional context in comments.

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.