Krishna priya v
Krishna priya v

Reputation: 87

Cloning of an array

I am cloning an array, normal adding functionality is working good. But when I delete some clone and then add a new clone then the last element in the array of clones is getting cleared in the UI but still existing in the DOM.

working code: https://stackblitz.com/edit/angular-j4ra7u

CURRENT BEHAVIOUR Consider we have three completely filled clones, and I delete the first clone and then add a new clone. While pushing the new clone into the array, the last filled clone in the array is getting cleared.

But this does not happen when I delete the last filled clone in the array. It works fine.

Upvotes: 1

Views: 71

Answers (2)

Krishna priya v
Krishna priya v

Reputation: 87

After trying so many workarounds, the code started working as expected when I removed the Form tag.

demo: https://stackblitz.com/edit/angular-v5xvzn

Please mention if there is any other way without removing the form tag.

Upvotes: 0

Matt McCutchen
Matt McCutchen

Reputation: 30949

I'm not sure if this is the problem you were talking about, but I noticed that when I delete all records, the record that gets automatically added is a copy of the record most recently added before that, instead of blank. This is because your newTemplateAdd method is allowing this.N_temp to remain aliased to an element of this.N_items, so data entered into the record gets saved in this.N_temp, and removeNewClone copies this.N_temp without reinitializing it.

Instead of doing this funny thing with this.N_temp, wouldn't it be simpler to just have a function that creates a new record each time you need one? See this demo.

Upvotes: 2

Related Questions