2015-08-03 75 views
1

我有一個對象數組,因此需要允許用戶添加一個與另一個項目相同的項目。當他們增加一個名爲'HasPers'的屬性爲真的數量時觸發它。在ng-repeat中重複一個項目

的HTML標記看起來像:

<td width="10%"><input type="number" value="{{productdetails.Quantity}}" ng-model="productdetails.Quantity" min="1" ng-change="change(productdetails)" /></td> 

而且功能:

$scope.change = function (item) { 
     if (item.HasPers) { 
      item.Quantity = 1; 
      $scope.items.push(item); 
     } 
     }; 

最初,這曾在wherby它知道這是一個duplicat對象的中繼器的問題,但這個被解決在中繼器中添加'track by $ index'

<tr ng-repeat="productdetails in items track by $index"> 

http://mutablethought.com/2013/04/25/angular-js-ng-repeat-no-longer-allowing-duplicates/

但是該項目顯然仍然與原始關聯,因爲當我更改原始項目上的某些屬性時,它們也會在重複項目上發生變化。我真的想避免再次輸入了所有的屬性,如

How to add new key in existing array in angular js?

有沒有什麼辦法來創建一個新的項目,對於與中傳遞的一個,但不同的對象?

+3

使用angular.copy到 – Hmahwish

+0

感謝複製原始項目。工作完美 – GrahamJRoy

+0

歡迎,您應該使用angular.copy從複製元素和添加到相同的來源。 – Hmahwish

回答

1

嘗試使用angular.copy

$scope.change = function (item) { 
    if (item.HasPers) { 
    var newItem = angular.copy(item); 
     newItem.Quantity = 1; 
     $scope.items.push(newItem); 
    } 
    }; 
3

嘗試

$scope.change = function (item) { 
    if (item.HasPers) { 
     var copyItem=angular.copy(item); 
     copyItem.Quantity = 1; 
     $scope.items.push(copyItem); 
    } 
    }; 
相關問題