javascript - 在每次 ng-click 时增加 ng-repeat 的计数

标签 javascript angularjs angularjs-ng-repeat

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}
  ]
};

jsfiddle

最佳答案

ng-click="count=count+1; addLike(todo._id, count)"

Fiddle

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/

相关文章:

javascript - 解除绑定(bind)涉及从 DOM 中删除 jquery 的点击事件

javascript - 如何在基于 jquery 的 jsfiddle 中使用 javascript?

javascript - 如何创建dom元素,取决于填充输入

javascript - Angular JS 从 1.1.0 升级到 1.1.1 后失败

javascript - PUPPETEER - 无法使用 page.evaluate(() => document.querySelectorAll()) 提取某些网站上的元素

javascript - d3js v5 调整分组条形图大小

javascript - 如何通过混合值来编写两个 ng-repeat?

javascript - AngularJS 计时器不会停止

javascript - 尝试在 AngularJS 中创建多重过滤器以按月份和年份 ID 过滤 JSON 数据

javascript - 使用 angular 和 ng-repeat 从 Ajax JSON 加载表