Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6在函数中更改对象值_Javascript_Angularjs_Ecmascript 6_Arrow Functions - Fatal编程技术网

Javascript ES6在函数中更改对象值

Javascript ES6在函数中更改对象值,javascript,angularjs,ecmascript-6,arrow-functions,Javascript,Angularjs,Ecmascript 6,Arrow Functions,我有一个角度控制器,它注入一个数组: class a{ constructor(items){ this.items = items; this.items[0].name = "abc"; } edit(){ this.items[0].name = "abc"; }) } 如果我要更改此数组中某个项的值,则在构造函数中,它将更改原始数组中该项的值 如果我在编辑方法中更改了该值,那么它现在在原始方法中不会更改

我有一个角度控制器,它注入一个数组:

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

    edit(){
        this.items[0].name = "abc";
    })
}
如果我要更改此数组中某个项的值,则在构造函数中,它将更改原始数组中该项的值

如果我在编辑方法中更改了该值,那么它现在在原始方法中不会更改

我能做什么


谢谢。

如果您使用angular,那么您应该使用
angular.copy
angular.extend
中分配
项目
。项目
,无需参考

var _this = this
_this.items =angular.copy(items);

取决于调用
编辑
的方式。从中调用编辑时,单击是否不应在类中进行编辑?显然,在其他地方会为该项指定一个新值
var\u this=this
不重要。在Angular Promissions中,数据位于
res.data
,因此您需要
res.data.name
已尝试过(对对象,而不是对整个数组),但不起作用。这与OP作者面临的问题无关。此外,何时为变量赋值取决于使用上下文。您不必每次都复制输入以将其分配给某个类属性。这甚至不是一个“经过深思熟虑的”最佳实践,所以这个答案根本没有解决这个问题。