step-1- Todo 值将增加每次点击对应的 todo
<li class="list-group-item" ng-repeat="todo in todos">
<span ng-click="count = count + 1" ng-init="count=0"> value= {{count}} </span>
</li>
第 2 步 - 如何在每次点击时将“计数”和待办事项“id”放入我的函数中
问题(问题在这里)
我的代码(我知道错了)
<li class="list-group-item" ng-repeat="todo in todos">
<span ng-click="addLike(todo._id) (count = count + 1)" ng-init="count=0"> value= {{count}} </span>
</li>
function TodoCtrl($scope) {
$scope.todos = [
{text:'todo one'},
{text:'todo two', done:false}
]
};
最佳答案
ng-click="count=count+1; addLike(todo._id, count)"
Bhesh Gurung 提供 a resource了解为什么增量运算符 (++
) 对我们不起作用:
Since ++points isn't a valid Angular expression, what happens? The helpful error message in the console points us in the right direction:
Error: [$parse:syntax] Syntax Error: Token '+' not a primary expression at column 2 of the expression [++points] starting at [+points]
From this message, we see that our expression used some unsupported syntax (in this case, the pre-increment operator ++). The error message is thrown by the very magical core Angular service, $parse.
还值得注意的是,每个按钮都有自己的范围,因此计数会独立增加。
关于javascript - 在每次 ng-click 时增加 ng-repeat 的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611072/