[英]How to set flag for object without affect other $scope value in angularjs?
Here I added sample for Move Items From One List to Another 在这里,我添加了将项目从一个列表移动到另一个列表的示例
While Am move the Item that time I need to set flag as 'D' for moving item from where I am moving and I need to set flag as 'A' for moved item. 当时Am移动项目时,我需要将标志从移动位置设置为'D',并且需要将标志设置为'A'来移动项目。 I tried but If I change the object both place it affected can any one help me on this... 我试过了,但是如果我同时更改两个对象的位置,受影响的任何人都可以帮助我...
for eg:- I tried to move Alpha left to right 例如:-我试图将Alpha从左向右移动
In left side object:- 在左侧对象中:-
$scope.items = [
{
"Key": 0,
"Description": "Alpha",
"flag": "D"
},
{
"Key": 1,
"Description": "Beta",
"flag": "E"
},
{
"Key": 2,
"Description": "Gamma",
"flag": "E"
},
{
"Key": 3,
"Description": "Delta",
"flag": "E"
},
{
"Key": 4,
"Description": "Epsilon",
"flag": "E"
}
];
In Right side object:- 在右侧对象中:-
$scope.items1 = [
{
"Key": 5,
"Description": "Zeta",
"flag": "E"
},
{
"Key": 6,
"Description": "Eta",
"flag": "E"
},
{
"Key": 0,
"Description": "Alpha",
"flag": "A"
}
];
This is because you are pushing the same object into the other array while movement and the updates are occurring on the object reference, so making changes on object in one array is also updating the same object in the other array. 这是因为在移动和更新发生在对象引用上时,您正在将同一对象推入另一个数组,因此对一个数组中的对象进行更改也会更新另一个数组中的同一对象。
As per your described requirement, you must first create a copy of the object before pushing into the array while movement, so you can replace the code as below: 根据您描述的要求,您必须先创建对象的副本,然后再移动时将其推入数组,因此可以替换以下代码:
$scope.array1.push(item);
with $scope.array1.push(angular.copy(item));
与$scope.array1.push(angular.copy(item));
and 和
$scope.array.push(item);
with $scope.array.push(angular.copy(item));
与$scope.array.push(angular.copy(item));
Here angular.copy
creates a deep copy of the object. 这里angular.copy
创建对象的深层副本。
Refer plnkr with above changes: http://plnkr.co/edit/at5x3hB9iq8DZCzpD5qP?p=preview 请通过上述更改参考plnkr: http ://plnkr.co/edit/at5x3hB9iq8DZCzpD5qP?p=preview
You just update an object add the same reference to another array and update this reference. 您只需更新一个对象,即可将相同的引用添加到另一个数组并更新该引用。 So the first update is discarded by the second. 因此,第一次更新将被第二次丢弃。
In your case you need to clone the object before adding it to the other array. 在您的情况下,您需要先克隆对象,然后再将其添加到另一个数组中。 You will need to search them by a distinctive attribute like an id. 您将需要通过独特的属性(例如ID)来搜索它们。
Take a look at this post for cloning object How do I correctly clone a JavaScript object? 看一下有关克隆对象的文章如何正确克隆JavaScript对象? or you can use angular.copy 或者您可以使用angular.copy
As others have explained it's because you are moving the same item between lists. 正如其他人所解释的,这是因为您要在列表之间移动同一项目。 A better solution would be to create a wrapper object. 更好的解决方案是创建一个包装对象。 That way you don't need to copy the entire structure of each item, but instead only need to create a small "changeitem" that you add to your lists. 这样,您无需复制每个项目的整个结构,而只需要创建一个添加到列表中的小的“更改项”即可。
So instead of adding the items themselves, move the flag attribute to an outer class and use that in your arrays instead. 因此,与其将这些项本身添加,不如将flag属性移动到外部类,并在数组中使用它。
var oldItem = $scope.array[index];
oldItem.flag = 'D';
var changedItem = { flag: 'A', item: oldItem.item }
$scope.array1.push(changedItem)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.