AngularJs:类似简单的系统

AngularJs:类似简单的系统,angularjs,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) { //li

是否有任何方法可以在单击时增加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++
}
  • ... ... Like{{post.Like_count} ... ...
  • $scope.postLike=函数(likeCount){ //利克蒙特++ }
    我可能会以某种方式使用
    $scope.$apply
    ,但我如何确切地知道递增哪个变量?

    比如{{post.Like_count}
    
    <button ng-click="postLike(post)">Like {{post.like_count}}</button>
    
    $scope.postLike = function(post) {
        post.like_count += 1;
    };
    
    $scope.postLike=函数(post){ post.like_计数+=1; };
    一种方法是:

    <button ng-click="postLike($index)">Like {{post.like_count}}</button>
    
    $scope.postLike = function(i) {
        $scope.posts[i].like_count += 1;
    };
    
    Like{{post.Like_count}
    $scope.postLike=函数(i){
    $scope.posts[i].like_count+=1;
    };
    

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

    只是想澄清一下:之所以这样做,是因为您将引用类型传递给函数并对其进行更改,而问题是传递一个基元类型。