我試圖用兩種方式填充輸入。第一種方法是簡單地在輸入中輸入數量,這很好地工作。第二種方法(我正在努力)是檢查ngRepeat指令中生成的複選框。當從ngRepeat檢查複選框時填充輸入
期望的行爲是複選框將從JSON數據中取得item.amount的值,並使用該值填充輸入。下面是標記:
<table class="table table-striped header-fixed" id="invoiceTable">
<thead>
<tr>
<th class="first-cell">Select</th>
<th class="inv-res2">Invoice #</th>
<th class="inv-res3">Bill Date</th>
<th class="inv-res4">Amount</th>
<th class="inv-res5">Amount to Pay</th>
<th class="inv-res6"></th>
</tr>
</thead>
<tbody>
<tr ng-if="invoices.length" ng-repeat="item in invoices | filter: {status:'Unpaid'}">
<td class="first-cell"><input type="checkbox" /></td>
<td class="inv-res2"><a href="invoices/{{item.invoiceNum}}">{{item.invoiceNum}}</a></td>
<td class="inv-res3">{{item.creationDate}}</td>
<td class="inv-res4" ng-init="invoices.total.amount = invoices.total.amount + item.amount">{{item.amount | currency}}</td>
<td class="inv-res5">$
<input ng-validate="number" type="number" class="input-mini" ng-model="item.payment" ng-change="getTotal()" step="0.01" /></td>
</tr>
</tbody>
</table>
<table class="table">
<tbody>
<tr class="totals-row" >
<td colspan="3" class="totals-cell"><h4>Account Balance: <span class="status-error">{{invoices.total.amount | currency }}</span></h4></td>
<td class="inv-res4"><h5>Total to pay:</h5></td>
<td class="inv-res5">{{total | currency}}</td>
<td class="inv-res6"></td>
</tr>
</tbody>
</table>
這裏是JavaScript:
myApp.controller('invoiceList', ['$scope', '$http', function($scope, $http) {
$http.get('assets/js/lib/angular/invoices.json').success(function(data) {
$scope.invoices = data;
});
$scope.sum = function(list) {
var total=0;
angular.forEach(list , function(item){
total+= parseInt(item.amount);
});
return total;
};
$scope.total = 0;
$scope.getTotal = function() {
$scope.total = 0;
$scope.invoices.forEach(function(item){
$scope.total += parseFloat(item.payment);
});
};
$scope.pushPayment = function(item){
if($scope.checked == 'checked'){
return item.payment;
}
};
}]);
是'checked'屬性不是布爾值嗎?如果是這樣,如果你將它設置爲'ng-model',它將返回true或false。 '$ scope'上也沒有'checked'屬性,這是沒有問題的,因爲它會自動添加到$ scope中,但它總是爲false。我想你想要'item.checked'或類似的東西。 –
你的'invoices'數據結構是什麼樣的? – Edd
很好的問題:[{ 「invoiceNum」: 「312490」, 「量」:14.20, 「creationDate」: 「2015年3月7日」, 「狀態」: 「付費」, \t 「支付」 :0 },... –