2014-09-30 58 views
0

我想就狀態,其中狀態值= 10 並顯示一個計數。AngularJS - 計數JSON以「X」價值

<div ng-app="" ng-controller="customersController"> 



    <table> 
     <tr ng-repeat="x in Number"> 
      <td>{{ x.number }}</td> 
      <td>{{ x.resource }}</td> 
      <td>{{ x.status }}</td> 
     </tr> 
    </table> 

</div> 
<script> 
function customersController($scope, $http) { 
    $http.get("mylocalhostlink") 
    .success(function (response) { $scope.Number = response }); 

} 
+0

那麼問題是什麼? – tymeJV 2014-09-30 15:42:44

+0

Sry我制定了這個問題很糟糕。我有3個不同的地位,有10-20和30個。我想在10,20和30上有多少個計數器,所以它會像狀態:30 = 100次,狀態:20 = 50次狀態:10 = 33次 – adam 2014-09-30 16:21:55

回答

1

你可能會想使用你的成功部分內angulars的foreach。在這裏的foreach文件 - link我創建這個小提琴,顯示我的意思 - link

代碼從下面的jsfiddle:

HTML:

<div ng-app="test" ng-controller="customersController"> 
<h1> Test </h1> 
<h4> Tens = {{tens}}</h4> 
<h4> Twenties = {{twenties}}</h4> 
<h4> Thirties = {{thirties}}</h4> 
<table> 
    <tr ng-repeat="x in Number"> 
     <td>{{ x.number }}</td> 
     <td>{{ x.resource }}</td> 
     <td>{{ x.status }}</td> 
    </tr> 
</table> 

JS:

angular.module('test', []) 
.controller('customersController', function ($scope) { 
    $scope.Number = [ 
     {number: 1, resource: 1, status: 10}, 
     {number: 1, resource: 1, status: 10}, 
     {number: 2, resource: 2, status: 20}, 
     {number: 3, resource: 3, status: 30} 
    ]; 

    // Set your variables to 0 at first 
    $scope.tens = 0; 
    $scope.twenties = 0; 
    $scope.thirties = 0; 

    // Use angulars for each to loop over your numbers 
    angular.forEach($scope.Number, function(value, key) { 
     // Increment each number by one when you hit it 
     if (value.status == 10){ 
      $scope.tens++ 
     }else if (value.status == 20){ 
      $scope.twenties++ 
     }else if (value.status == 30){ 
      $scope.thirties++ 
     } 
    }); 
}); 
+0

Sry我制定了這個問題很糟糕。我有3個不同的地位,10-20和30.我想做一個計數器,我有10,20和30多少,所以它會像 狀態:30 = 100次, 狀態:20 = 50次 狀態:10 = 33次 – adam 2014-09-30 15:52:55

+0

您可能會希望在您的成功部分中使用angulars foreach。在這裏的foreach文件 - [鏈接](https://docs.angularjs.org/api/ng/function/angular.forEach) 我創建這個小提琴,顯示我的意思 - [鏈接](HTTP:/ /jsfiddle.net/7a9wdv8q/22/) – Profane 2014-10-01 11:44:42

+0

非常感謝。我想我現在可以解決它, – adam 2014-10-01 12:10:26

-1
<div ng-controller="customersController" ng-init="memo={}"> 
    <div ng-repeat="x in Number" ng-init="memo[x.status] = memo[x.status] + 1"></div> 
    <table> 
     <tr ng-repeat="(k, v) in memo"> 
      <td>{{ k }}</td> 
      <td>{{ v }}</td> 
     </tr> 
    </table> 
</div>