Greetings

We have about the following:

A model of, say, goods:

this.trade_model = [ { name: 'name1', title: 'title_one', cost: 1000, ... }, ... ]; 

Fields such as name , title , etc.. can be set.

On click we show <input ng-model="needed_object.name"> , which rules the name object of our model.

How to give a person a new value, then after pressing the "OK" button, apply the value to the model field, or after pressing the "CANCEL" button, reset the entered values.

I apologize if it is not clear explained :)

    1 answer 1

    Make editing the copy of the desired element, and not directly the element of the array.

     this.trade_model = [ { name: 'name1', title: 'title_one', cost: 1000, ... }, ... ]; this.editIs = false; this.editIndex = -1; this.editObject = null; this.edit = function (index) { if (!this.trade_model[index]) { return; } this.editIs = true; this.editIndex = index; this.editObject = angular.copy(this.trade_model[index]); }; this.editCancel = function () { this.editIs = false; this.editIndex = -1; this.editObject = null; }; this.editDone = function () { this.trade_model[this.editIndex] = angular.copy(this.editObject); this.editCancel(); }; 

    Or, on the contrary, save the original, so that if necessary, roll back to it and edit directly in the array.

    • I apologize for the long reaction, but I don’t quite understand how to implement it, therefore I mentioned the field <input ng-model="needed_object.name"> Could you tell us more in detail how to use these functions? - Nikolay
    • Your needed_object in my case, this is this.editObject . It is also written По клику показываем ... Those. call this.edit(index) , which creates an object for us to edit this.editObject . After that, we click Сохранить this.editDone() and call this.editDone() , which replaces the object in the array with the modified one, or we call this.editCancel() , and discard the changes. - Alexander Anikeev
    • one
      you have an extreme function called editDone index ( this.trade_model[index] ), I suppose you just missed it in the function variable passed? - Nikolay
    • @ Nikolai yes, you are right. Only from the saved this.editIndex . Edited - Alexander Anikeev