我需要根據表格單元格變化中輸入框的值更新觸發器更新。該表使用ng-repeat與$ resource調用的結果進行比較。
關鍵是我需要每次更改時更新2個不同的值。 1是同一表格行內的另一個單元格,第二個單元格位於表格本身之外。這似乎阻止在ng-repeat級別使用特定的控制器。
我創建了這個plunkr來試圖提供幫助。 http://plnkr.co/edit/773WhoUvWoUZ40AfuzMO?p=preview
// Code goes here
angular.module('plunker', [])
.controller('ItemCtrl', function ($scope) {
$scope.user_votes = 100;
$scope.items = [
{item: "Foo1", description: "Really Fancy Foo", total_votes: 0, my_votes: 0},
{item: "Foo2", description: "Boring Foo", total_votes: 0, my_votes: 0},
{item: "Widget 1", description: "Base widget 1", total_votes: 0, my_votes: 0},
{item: "Big Widget", description: "Humongous Widget", total_votes: 0, my_votes: 0},
{item: "FooBar", description: "FooBar in Gold", total_votes: 0, my_votes: 0}
];
// This doesn't work, but I am not sure why.
$scope.$watch('item.my_votes', function(newVal, oldVal){
$scope.user_votes = $scope.user_votes - newVal.my_votes;
newVal.total_votes = newVal.total_votes + newVal.my_votes;
});
});
HTML代碼是所有的
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="http://code.angularjs.org/1.1.5/angular.min.js" data-semver="1.1.5"> </script>
<script src="script.js"></script>
</head>
<body ng-controller="ItemCtrl">
<div>You have {{ user_votes }} votes remaining</div>
<table>
<thead>
<tr>
<th>Item</th>
<th>Description</th>
<th>My Votes</th>
<th>Total Votes</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{ item.item }}</td>
<td>{{ item.description }}</td>
<td><input name="votes" ng-model="item.my_votes"/></td>
<td>{{ item.total_votes }}</td>
</tr>
</tbody>
</table>
<button ng-click="saveForm">Cast Votes</button>
</body>
</html>