限制AngularJS中的ng-repeat迭代

Ash*_*yal 1 javascript angularjs

我有嵌套ng-repeat像这样的东西.我想将它限制为仅使用列表中的前两项(而不是完整列表).

ng-repeat = "items in phone.comments 
Run Code Online (Sandbox Code Playgroud)

和HTML:

      <div class="wallList clearfix" ng-repeat = "items in phone.comments | limitTo:quantity">
        <div class="wallListImage">
          <a href="#">
            <img ng-show = "items.imageUrl" ng-src="{{items.imageUrl}}" height = "42px" width= "42px"/>
            <img ng-show = "!items.imageUrl" ng-src="WishlistImage/movies.png" height = "42px" width= "42px"/>
          </a>
        </div>
      </div>
Run Code Online (Sandbox Code Playgroud)

Ale*_*son 5

我看起来你走在正确的轨道上.假设您已经定义了$ scope.quantity,那么您的代码应该可行.就像你的例子中一样,我建议使用'limitTo'过滤器.

它的使用示例:

<div ng-repeat="item in items | limitTo:2">
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到一个实例:

http://jsfiddle.net/Nu7rZ/

这里有一个类似的Stack Overflow问题:

ng-repeat定义次数而不是重复数组?