0

I have angular controller, which injects an array:

class a{
    constructor(items){
        this.items = items;
        this.items[0].name = "abc";
    }

    edit(){
        this.items[0].name = "abc";
    })
}

If I will change the value of one item in this array, inside the constructor, it will change the value of the item in the original array.

If I change the value inside the edit method, it will not change now in the original.

What can I do?

Thanks.

user3712353
  • 2,741
  • 4
  • 13
  • 31
  • 1
    `this` depends on how `edit` is called – Jaromanda X Feb 04 '16 at 10:44
  • It is called from angular ng-click – user3712353 Feb 04 '16 at 10:46
  • 1
    Shouldn't edit be inside the class? – yeouuu Feb 04 '16 at 10:48
  • 1
    Obviously somewhere else a new value is assigned to `this.items`. `var _this = this` doesn't matter. – a better oliver Feb 04 '16 at 11:27
  • 1
    In Angular promises, the data is in `res.data`, so you want `res.data.name` – Alnitak Feb 04 '16 at 11:57
  • I am sure your class definition is perfectly fine and should work. The problem is either that your `res.name` property is the same as the old value for that property, so the change is happening but you're not observing it, or, you're not calling the edit method right on the instance but instead you're calling it by reference. var instance = new a(someItems); var edit = instance.edit; edit() // here this will not refer to the class instance anymore – eAbi Feb 04 '16 at 12:07
  • @Alnitak it is for the example, it doesn't matter for this question. – user3712353 Feb 04 '16 at 12:08
  • Possible duplicate of [how `this` works in javascript](http://stackoverflow.com/a/3127440/2135910). @user3712353 Please either review the excelent answer from there and make sure you know how `this` works in javascript, or provide more informations on what is wrong with your code, what is the error you're receiving or how can it be reproduced. – eAbi Feb 04 '16 at 12:19
  • Hi. I've seen the answer. Anyway, ES6 claims that arrow function gets the parent scope. I guess it copies the scope and not reference to it, thats why I cant change the original value from the arrow function. Hope someone will know the answer. – user3712353 Feb 04 '16 at 16:59
  • You already have the answer. The problem is not this code. – a better oliver Feb 05 '16 at 08:30

1 Answers1

0

If you use angular then you should use angular.copy or angular.extend to assign items in _this.items without reference

var _this = this
_this.items =angular.copy(items);
Shaishab Roy
  • 14,345
  • 4
  • 44
  • 63
  • Already tried it (to the object, not to the whole array), didn't work. – user3712353 Feb 04 '16 at 11:06
  • This has nothing to do with the problem the OP's author is facing. Also, when assigning a value to a variable depends on the usage context. You don't have to copy the input every time to assign it to some class property. It's not even a "considered" best practice, so this answer doesn't address the problem at all. – eAbi Feb 04 '16 at 12:03