2017-07-31 85 views
0

我有一個文本框遞增或遞減被重複循環的文本框的值:如何使用角JS

<div class="container"> 
    <div ng-repeat="x in checkoutData"> 
    <img src="img/minus.jpg" ng-click="Decrement();"> 
    <input value="1" type="text" ng-model="items[$index]"/> 
    <img src="img/plus.jpg" ng-click="Increment($index);"> 
</div> 
</div> 

我的角碼是:

$scope.Increment = function(index) { 
    alert($scope.items[index]); // TypeError: Cannot read property '0' of undefined 
}; 

因爲文本和圖像重複着。如果我擊中特定的圖像只有特定的文本框的值必須增加也反過來遞減。我如何使用Angular JS做到這一點?

+0

你應該爲每個輸入定義單獨'model'。像'

+0

嘗試改變你的ng模型,比如x.value。現在所有的輸入都綁定到相同的ng模型值 – Vivz

+0

@Vivz值將保持相同1爲所有文本框.. –

回答

0

當您嘗試增加計數器時,$scope.items尚未初始化。裏面的Increment功能,添加null/undefined檢查和值設置爲0

$scope.Increment = function(index) { 
    if(!$scope.items[index]) { 
     $scope.items[index] = 0; 
    } 
    $scope.items[index]++; 
    }; 

Working Plunker

+0

感謝隊友的工作.. –