2015-06-15 45 views
0

我有一個控制器和控制我循環
NG點擊=「minusOne($指數)」NG點擊=「Plusone精選($指數)」,但如果我點擊第一個阻止其改變所有的迭代,但我想,如果我第一個塊單擊它應該首先塊僅上點擊的所有項目一起是在迭代工作 - angularjs

 .controller('MainController', ['$scope', '$rootScope', function($scope, $rootScope) { 

      $scope.fine = [{ 
       likes: 0, 
       dislikes: 0 
      }]; 
      $scope.plusOne = function(index) { 
       $scope.fine[index].likes += 1 * 10; 
      }; 
      $scope.minusOne = function(index) { 
       $scope.fine[index].dislikes += 1 * 10; 
      }; 
      $scope.buildBreak = function(index) { 
       $scope.fine[index].likes += 1 * 50; 
      }; 

     }]) 

的Html

上工作
<div class="item item-body addfineinfo" ng-repeat="putfine in fine"> 

    <a ng-click="plusOne($index)" class="button button-small button-dark">+ Fine</a> 
    <a ng-click="minusOne($index)" class="button button-small button-dark">- Fine</a> 
    <a ng-click="buildBreak($index)" class="button button-small button-assertive">BUILD BREAK</a> 
<input name="message" readonly type="text" value="{{ putfine.likes - putfine.dislikes }}" placeholder="Suhr"> 

值應該來在這個領域,這也是互爲作用

+2

似乎是什麼問題? - > http://plnkr.co/edit/49YpzoEZHjMUWGV2zSNb?p=preview – chris

+0

我沒有看到有任何問題 – simon

+0

同意w /克里斯...代碼似乎很好。在上面的示例中,只顯示數組中的一個項目。你如何在數組中放置多個項目?例如,如果數組填充了w /對同一對象的引用,則可能會出現您描述的問題。 –

回答

1

HTML

 <!DOCTYPE html> 
<html> 

    <head> 
    <script data-require="[email protected]" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular.js"></script> 
    <link rel="stylesheet" href="style.css" /> 
    <script src="script.js"></script> 
    </head> 

    <body> 
    <div ng-app="myApp" ng-controller="ctrl"> 


<div ng-repeat="developerAdd in developer"> 
{{ developerAdd.likes}} - {{developerAdd.dislikes }} 
<br/> 
<a ng-click="plusOne($index)" class="button button-small button-dark">+ Fine</a> 
<a ng-click="minusOne($index)" class="button button-small button-dark">- Fine</a> 
<a ng-click="buildBreak($index)" class="button button-small button-assertive">BUILD BREAK</a> 

</div> 
</div> 
    </body> 

</html> 

控制器

// Code goes here 

var app = angular.module('myApp', []); 

app.controller('ctrl', function($scope) { 

    $scope.developer = [{ 
     id: 1 
    },{ 
     id: 2 
    },{ 
     id: 3 
    },{ 
     id: 4 
    }]; 

    $scope.plusOne = function(index) { 
    if ($scope.developer[index].likes == undefined) $scope.developer[index].likes = 0; 
     $scope.developer[index].likes += 1 * 10; 
    }; 

    $scope.minusOne = function(index) { 
    if ($scope.developer[index].dislikes == undefined) $scope.developer[index].dislikes = 0; 
     $scope.developer[index].dislikes += 1 * 10; 
    }; 

    $scope.buildBreak = function(index) { 
    if ($scope.developer[index].likes == undefined) $scope.developer[index].likes = 0; 
     $scope.developer[index].likes += 1 * 50; 
    }; 
}); 
+0

它不能正常工作 – supersaiyan

+0

你可以創建一個plunker並告訴我什麼是問題。 – simon

+0

在上面的評論中的蹦牀很好,但它的工作只爲2次迭代和迭代可以是任何 – supersaiyan

相關問題