2014-09-20 60 views
1

我试图让每个iteration.my代码项目的总人数是这个我如何获得项目总的NG-重复

<ul class="phones"> 
     <li ng-repeat="phone in phones | filter:query | orderBy:orderProp" 
     ng-init="index()"  class="thumbnail"> 
     <a href="#/phones/{{phone.id}}" class="thumb"> 
      <img ng-src="{{phone.imageUrl}}"> 
     </a> 
      <a href="#/phones/{{phone.id}}">{{phone.name}}</a> 
     <p>{{phone.snippet}}</p> 
     </li> 
    </ul> 
    <div> 
    {{ total }} 
</div> 

控制器

phonecatApp.controller('PhoneListCtrl', function($scope, $http) 
{ 
$scope.total =0; 
$scope.index =function() 
{ 
    $scope.total ++; 
} 
} 

回答

3

通过动态你的意思是应用过滤器后,手机总没有当前显示。

<ul class="phones"> 
    <li ng-repeat="phone in filtered = (phones | filter:query | orderBy:orderProp)" 
    ng-init="index()"  class="thumbnail"> 
    <a href="#/phones/{{phone.id}}" class="thumb"> 
     <img ng-src="{{phone.imageUrl}}"> 
    </a> 
     <a href="#/phones/{{phone.id}}">{{phone.name}}</a> 
    <p>{{phone.snippet}}</p> 
    </li> 
</ul> 
<div> 
{{ filtered.length }} 
</div> 

为了使更多的数据的意义(奖金提示:))

<div ng-show="query"> 
    <ng-pluralize count="filtered.length" when="{'0': ' No search result ', 'one': ' 1 search result ', 'other': ' {} search results '}"></ng-pluralize> for {{query}} 
</div> 
1

你是不是想获得手机总数? 你可以简单地使用length属性的表达,而不是{{ total }}

{{ phones.length }} 
+0

不完全是,我想的时候NG-重复重复了“总”(值),动态的数量。 – 2014-09-20 20:44:56