AngularJs:简单就像系统一样

时间:2014-03-31 00:42:03

标签: angularjs

有没有办法在点击时增加ng-repeat循环中的变量?

<li class="item" ng-repeat="post in posts">
...
... 
<button ng-click="postLike(post.like_count)">Like {{post.like_count}}</button>
...
...
</li>

$scope.postLike = function(likeCount) {
    //likeCount++
}

我可能会以某种方式使用$scope.$apply,但我怎么知道要增加哪个变量?

2 个答案:

答案 0 :(得分:1)

<button ng-click="postLike(post)">Like {{post.like_count}}</button>

$scope.postLike = function(post) {
    post.like_count += 1;
};

答案 1 :(得分:1)

这样做的方法如下:

<button ng-click="postLike($index)">Like {{post.like_count}}</button>

$scope.postLike = function(i) {
    $scope.posts[i].like_count += 1;
};

您可以直接更改posts数组中的值。

相关问题