2016-10-20 6 views
1

ng-repeat内の値をどのように置き換えることができますか。ng repeat内のオブジェクトを置き換えます。

<div ng-repeat="item in test"> 
    <input type="text" data-ng-model="item.qty"> 
</div> 

$scope.test = [ 
     {"InventoryItemID":78689,"Location":"My Location",qty:"2"}, 
     {"InventoryItemID":78689,"Location":"My Location",qty:"1"} 
    ] 

ここで、テスト数量をテスト数量に置き換える必要があります。どうやってやるの。

$scope.test1 = [ 
      {qty:"6"}, 
      {qty:"6"} 
     ] 
+0

私は私はあなたの理解かどうかわからないんだけどあなたはそれを試して言い換えることができますか? – Jordumus

+0

彼らは同じインデックスを持っていますか? – taguenizy

+0

@Jordumus:$ scope.test qtyの値を$ scope.test1と置き換える必要があります。数量 – Felixerity

答えて

2

を使用し、testtest1など一時的な値を使用することができます

<div ng-repeat="item in items"> 
    <input type="text" data-ng-model="item.qty"> 
</div> 

var test = [ 
     {"InventoryItemID":78689,"Location":"My Location",qty:"2"}, 
     {"InventoryItemID":78689,"Location":"My Location",qty:"1"} 
    ] 

var test1 = [ 
      {qty:"6"}, 
      {qty:"6"} 
     ] 

$scope.items = test; 

はその後、それを交換するだけの操作を行います。それを行うための「角道」になり

$scope.items = test1; 


ライブ、それを参照してください:

angular.module('App', []) 
 
.controller('Ctrl', function($scope){ 
 
    $scope.test = [ 
 
     {"InventoryItemID":78689,"Location":"My Location",qty:"2"}, 
 
     {"InventoryItemID":78689,"Location":"My Location",qty:"1"} 
 
    ] 
 

 
    $scope.test1 = [ 
 
      {qty:"6"}, 
 
      {qty:"6"} 
 
     ] 
 

 
    $scope.items = $scope.test; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="App" ng-controller="Ctrl"> 
 
    <div ng-repeat="item in items"> 
 
    <input type="text" data-ng-model="item.qty"> 
 
    </div> 
 
    <br> 
 
    <button ng-click="items = test">Use Test</button> 
 
    <button ng-click="items = test1">Use Test1</button> 
 
    <br><br> 
 
    {{items}} 
 
</div>


EDIT

私は質問を読み違えてきたかもしれません。

あなただけの数量を更新する場合は、例えば、古い学校は、forループ、ちょうど他のJavaScriptオブジェクトと同じようにそれを行うことができます。

for (var i = 0; i < $scope.test.length; i++){ 
    $scope.test[i].qty = $scope.test1[i].qty; 
} 
+0

オブジェクト全体ではなく、代わりに数量だけ必要な場合はどうすればいいですか? – Felixerity

+0

それについての更新を書いていただけです。 :) – Shomz

2

次の例のように、あなたはあなたが必要とする値を保持する一つのオブジェクトを持つことができますObject.assign

$scope = this; // Ignore this line 
 

 
$scope.test = [ 
 
     {"InventoryItemID":78689,"Location":"My Location",qty:"2"}, 
 
     {"InventoryItemID":78689,"Location":"My Location",qty:"1"} 
 
    ] 
 

 
$scope.test1 = [ 
 
    {qty: "6"}, 
 
    {qty: "6"} 
 
] 
 

 
$scope.test.forEach(function(value, index){ 
 
    Object.assign(value, $scope.test1[index]) 
 
}) 
 

 
console.log($scope.test)

関連する問題