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

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

问题描述

我希望在按钮点击事件通过事件参数发生时指定一些值:

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

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

我已将用户的值分配给 $ scope.master

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

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

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

两者都是一样的,那有什么区别?请告诉我 angular.copy()等于(=)之间的区别。

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

推荐答案

可以在这里阅读 angular.copy()执行参数的深层复制(参见克隆) - 基本上创建一个新对象 - 而使用赋值运算符 = 只需指定引用

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.

因此在后一种情况下,如果我们要在 $ scope.master 中更改某些内容,你也会更改上下文

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

干杯,

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

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