2015-09-05 68 views
0

我用ng-repeat生成了一個列表,其中每個項目都有一個計數變量。在每個列表項目中,我都有一個鏈接。單擊ng-repeat中的項目時,如何增加計數器?

我想單擊鏈接時增加計數。

我試過以下的方法,但它不工作。

我的控制器: -

myApp.controller('allpost', function ($scope, $http, $stateParams, Allposts) { 
    var id = $stateParams.id; 
    Allposts.GetAllposts(id).then(
     function (response) { 
      $scope.allPosts = response.data.posts; 
     }); 

    function ctrl($scope) { 
      $scope.increment = function(item){ 
      item.count += 1; 
      } 
     } 
}) 

和查看,如: -

<ion-content class="padding" lazy-scroll> 
<div class="row no-padding HomeRowsList"> 

<div class="item itemfull" ng-repeat="post in allPosts"> 
     <div class="item item-body"> 
      <div> 
       <div class="title-news"> 
        <div class="title" ng-bind-html="post.content"></div> 
        <div class="countbg">عدد المرات : {{post.custom_fields.azkarno}}</div> 
        <span>{{post.count}}</span><a onclick="ctrl(post);">Increment</a> 
       </div> 
      </div> 
     </div> 
    </div> 

</div> 
</ion-content> 
+0

您還沒有生成*列表*,你生成了一堆unsemantic,嵌套的'div's的。 – connexo

回答

0

在控制器使用的

$scope.increment = function(item){ 
    item.count += 1; 
}; 

代替

function ctrl($scope) { 
    $scope.increment = function(item){ 
    item.count += 1; 
    } 
} 

和在HTML使用

<span>{{post.count}}</span><a data-ng-click="increment(post)">Increment</a> 

,而不是

<span>{{post.count}}</span><a onclick="ctrl(post);">Increment</a> 
0

應該ng-click而不是onclick &方法名稱應該是increment而不是ctrl

也可以從increment方法中刪除不必要的ctrl函數包裝器,因爲無論你想從html調用什麼都應該包含在控制器的$scope中。

標記

<span>{{post.count}}</span><a ng-click="increment(post);">Increment</a> 
+0

@ Android-wd有沒有我的答案有問題? –