我在 ng-repeat 中有两个输入字段 b_price 和 s_price。我需要观察这两个模型的变化并附加估计。我的代码片段:html
<body ng-app="testthis" ng-controller="testCtrl">
<table>
<tr ng-repeat="list in testList.lists track by $index">
<td>{{list.symbol}}</td>
<td>{{list.todays_rate}}</td>
<td><input type="text" ng-model="testList.b_price"></td>
<td><input type="text" ng-model="testList.s_price"></td>
<td>{{estimated}}</td>
</tr>
</table>
</body>
js
var app = angular.module('testthis', []);
app.controller('testCtrl', function($scope, $http) {
$scope.testList ={};
$scope.testList.lists = [{id: 1,symbol: "assas", total: 123},
{id: 2,symbol: "as", total: 103}];
for (var i=0; i<$scope.testList.lists.length; i++){
$scope.estimated =0;
$scope.$watch('testList.b_price + testList.s_price', function() {
console.log('watch');
if ($scope.testList.b_price){
console.log('as');
$scope.testList.s_price = "";
$scope.estimated = 100 - $scope.testList.b_price;
}else if ($scope.testList.s_price){
$scope.testList.b_price = "";
$scope.estimated = $scope.testList.s_price - 100;
}
});
}
});
当我在第一行的输入标签中输入值时,我会在第二行的输入中重复它。这可以使用 ng-model="testList.b_price[$index]" 修复。但我无法查看此值,即 $scope.testList.b_price[0], $scope.testList.b_price[i] 在 $watch 中给出错误。还有一个问题,我无法附加估计为估计[0],估计[2]。小提琴:http: //jsfiddle.net/sanj9090/b07jkstj/