我在左边有按钮的字段列表(todos)。我想单击其中一个按钮,以便计算此特定字段的值。AngularJS - 计算每个字段的值
什么我得到的是点击特定按钮计算并显示所有字段相同的结果
这是我写的:
<h1 ng-show="myVar">
<ul>
<li ng-repeat="todo in todos">
<button ng-show="!editing[$index]" ng-click="edit($index)">click</button>
<h2> Result is:{{result}}</h2>
{{todo.name}}
</li>
</ul>
</h1>
和控制器
$scope.edit = function(index){
var todo = $scope.todos[index];
var counter = 0;
angular.forEach($scope.todos,function(value,index){
if (todo.topic == 'important')
{counter = counter+1;}
})
$scope.result = counter;
}
我在做什么错?
这非常有帮助!现在它计算出正确的结果。 唯一的是我看到所有字段旁边的结果。我怎样才能改变它,结果只会显示在特定的领域,而不是每个人? –
@ A.Sh然后传递该特定对象来编辑像ng-click =“edit(todo)”这样的方法,并将结果属性添加到todo对象中,这样就可以通过使用'todo.result '。 –
todo是来自db的字段...你是否要将结果属性添加到数据库?因为我不想更改分贝。我可以做不同吗? –