angular.copy() 和赋值 (=) 有什么区别? [英] What is the difference between angular.copy() and an assignment (=)?

查看:32
本文介绍了angular.copy() 和赋值 (=) 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过事件参数在按钮单击事件发生时分配一些值:

$scope.update = 函数(上下文){$scope.master = 上下文;};

我已将 user 值分配给 $scope.master.

现在我看到了 angular.copy().所以我用 angular.copy 编写了代码.

$scope.update = 函数(上下文){$scope.master = angular.copy(context)};

两者都做同样的事情,那么有什么区别呢?请告诉我 angular.copy()equal(=) 之间的区别.

解决方案

As can be 阅读此处

a> angular.copy() 执行参数的深层复制(参见克隆")——本质上是创建一个新对象——而使用赋值运算符 = 只是赋值参考.

因此在后一种情况下,如果您要更改 $scope.master 中的某些内容,您也将更改 context.

干杯,

I want to assign some values when a button click event happens via event parameter:

$scope.update = function(context) {
    $scope.master = context;
};

I have assigned user values to $scope.master.

Now i am seeing angular.copy(). So I wrote the code with angular.copy.

$scope.update = function(context) {
    $scope.master = angular.copy(context)
};

Both are doing same, so what is the difference? Please tell me about the difference between angular.copy() and equal(=).

解决方案

As can be read here angular.copy() performs a deep copy (cf. "clone") of the argument - essentially creating a new object - whereas using the assignment operator = just assigns reference's.

Thus in the latter case, if you we're to change something in $scope.master you would also change context.

Cheers,

这篇关于angular.copy() 和赋值 (=) 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆