我有一个简单的输入列表,绑定到一个很好显示的项目列表。当我更改输入中的值时, 总和不会更新?
示例:http://plnkr.co/edit/B7tEAsXSFvyLRmJ5xcnJ?p = preview
HTML
<body ng-controller="MainCtrl"> <div ng-repeat="item in items"> <p><input type=text value="{{item}}"></p> </div> Total: <input type=text value="{{sum(items)}}"> </body>
脚本
app.controller('MainCtrl', function($scope) { $scope.items = [1,2,5,7]; $scope.sum = function(list) { var total=0; angular.forEach(list , function(item){ total+= item; }); return total; } });
检查此提琴是否实现:http : //jsfiddle.net/9tsdv/
需要注意的要点:
HTML:
<body ng-controller="MainCtrl"> <div ng-repeat="item in items"> <p><input type=text ng-model="item.value"></p> </div> Total: <input type=text value="{{sum(items)}}"> </body>
控制器代码:
app.controller('MainCtrl', function($scope) { $scope.items = [ { value: 1 }, { value: 2 }, { value: 5}, { value: 7 } ]; $scope.sum = function(list) { var total=0; angular.forEach(list , function(item){ total+= parseInt(item.value); }); return total; } });