2014-03-31 57 views
0

有没有什么办法在点击时增加ng-repeat循环内的变量?AngularJs:简单的系统

<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莫名其妙,但我怎么知道到底递增哪些变量?

回答

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

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

只是为了澄清:为什么这个工程的原因是你正在传递一个引用类型的函数,并改变它,问题是传递一个原始类型。 –

1

这样做的方法是如下:

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

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

,您可以直接改变帖数组中的值。