angular.copy()和分配之差(=) [英] Difference between angular.copy() and assignment (=)

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

问题描述

我要分配一些值时,通过事件参数按钮单击事件

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

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

我已指定用户 $ scope.master

现在我看到 angular.copy()。所以我写了code。与 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之间的差值()等号(=)

推荐答案

既然可以这里 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天全站免登陆