admin管理员组文章数量:1391975
It is easier to see the question from this plunkr:
Click on 'Click to add first' and 'Click to add second', then you can click on the 'lock' icon to see some of the items seem to have the same scope (or same ng-model).
and then click on 'Click to add third' this action does an angular.copy, it does not share the same scope with the other 2. Why is that?
How do I separate the scope so that I can have each 'lock' icon only apply on itself, not other items?
It is easier to see the question from this plunkr: http://plnkr.co/edit/EFZCAXWFui0foMbfZkPb?p=preview
Click on 'Click to add first' and 'Click to add second', then you can click on the 'lock' icon to see some of the items seem to have the same scope (or same ng-model).
and then click on 'Click to add third' this action does an angular.copy, it does not share the same scope with the other 2. Why is that?
How do I separate the scope so that I can have each 'lock' icon only apply on itself, not other items?
Share Improve this question asked Jan 18, 2014 at 1:06 Annie CAnnie C 8142 gold badges14 silver badges32 bronze badges1 Answer
Reset to default 5In this line:
var row = {"groupname":Math.floor(Math.random() * 9999999) + 1};
you create a new object and you create a reference to this object which is stored in the variable row
. You only add this reference (not a copy of the row
object) to your arrays, so the elements in both arrays point to the same object.
angular.copy creates a "deep copy" of your array, so all included objects will be copied and the array contains references to these new objects.
If you want to have separate objects, use angular.copy in the clickFirst function to duplicate the object:
var row = {"groupname":Math.floor(Math.random() * 9999999) + 1};
$scope.products1.push(row);
$scope.products2.push(angular.copy(row));
本文标签: javascriptdifferent scope for array and angular copyStack Overflow
版权声明:本文标题:javascript - different scope for array and angular copy - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744746578a2622918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论