Way to ng-repeat defined number of times instead of repeating over array?

前端 未结 26 3230
刺人心
刺人心 2020-11-22 14:53

Is there a way to ng-repeat a defined number of times instead of always having to iterate over an array?

For example, below I want the list item to show

相关标签:
26条回答
  • 2020-11-22 15:35

    angular gives a very sweet function called slice.. using this you can achieve what you are looking for. e.g. ng-repeat="ab in abc.slice(startIndex,endIndex)"

    this demo :http://jsfiddle.net/sahilosheal/LurcV/39/ will help you out and tell you how to use this "making life easy" function. :)

    html:

    <div class="div" ng-app >
        <div ng-controller="Main">
            <h2>sliced list(conditional NG-repeat)</h2>
            <ul ng-controller="ctrlParent">
                <li ng-repeat="ab in abc.slice(2,5)"><span>{{$index+1}} :: {{ab.name}} </span></li>
            </ul>
            <h2>unsliced list( no conditional NG-repeat)</h2>
             <ul ng-controller="ctrlParent">
                <li ng-repeat="ab in abc"><span>{{$index+1}} :: {{ab.name}} </span></li>
            </ul>
    
        </div>
    

    CSS:

    ul
    {
    list-style: none;
    }
    .div{
        padding:25px;
    }
    li{
        background:#d4d4d4;
        color:#052349;
    }
    

    ng-JS:

     function ctrlParent ($scope) {
        $scope.abc = [
         { "name": "What we do", url: "/Home/AboutUs" },
         { "name": "Photo Gallery", url: "/home/gallery" },
         { "name": "What we work", url: "/Home/AboutUs" },
         { "name": "Photo play", url: "/home/gallery" },
         { "name": "Where", url: "/Home/AboutUs" },
         { "name": "playground", url: "/home/gallery" },
         { "name": "What we score", url: "/Home/AboutUs" },
         { "name": "awesome", url: "/home/gallery" },
         { "name": "oscar", url: "/Home/AboutUs" },
         { "name": "american hustle", url: "/home/gallery" }
        ];
    }
    function Main($scope){
        $scope.items = [{sort: 1, name: 'First'}, 
                        {sort: 2, name: 'Second'}, 
                        {sort: 3, name: 'Third'}, 
                        {sort: 4, name:'Last'}];
        }
    
    0 讨论(0)
  • 2020-11-22 15:35

    I needed a more dynamic solution to this - where I could increment the repeat.

    HTML

    <div ng-repeat="n in newUserCount">
    <input type="text" value="" name="newuser{{n}}"/>
    </div>
    

    Duplicator Control

    <span class="helper" ng-click="duplicateUser()">
    Create another user with the same permissions
    </span>
    

    JS

     $scope.newUserCount = Array('1');
    var primaryValue = 1;
    $scope.duplicateUser = function()
    {
        primaryValue++;
        $scope.newUserCount.push(primaryValue)
    }
    
    0 讨论(0)
  • 2020-11-22 15:36

    Update (9/25/2018)

    Newer versions of AngularJS (>= 1.3.0) allow you to do this with only a variable (no function needed):

    <li ng-repeat="x in [].constructor(number) track by $index">
        <span>{{ $index+1 }}</span>
    </li>
    
    $scope.number = 5;
    

    This was not possible at the time the question was first asked. Credit to @Nikhil Nambiar from his answer below for this update


    Original (5/29/2013)

    At the moment, ng-repeat only accepts a collection as a parameter, but you could do this:

    <li ng-repeat="i in getNumber(number)">
        <span>{{ $index+1 }}</span>
    </li>
    

    And somewhere in your controller:

    $scope.number = 5;
    $scope.getNumber = function(num) {
        return new Array(num);   
    }
    

    This would allow you to change $scope.number to any number as you please and still maintain the binding you're looking for.

    EDIT (1/6/2014) -- Newer versions of AngularJS (>= 1.1.5) require track by $index:

    <li ng-repeat="i in getNumber(number) track by $index">
        <span>{{ $index+1 }}</span>
    </li>
    

    Here is a fiddle with a couple of lists using the same getNumber function.

    0 讨论(0)
  • 2020-11-22 15:36

    This is only a slight variation on the accepted answer, but you don't really need to create a new function. Only to import 'Array' in the scope:

    <div ng-app="myapp">
        <div ng-controller="ctrlParent">
            <ul>
                <li ng-repeat="i in counter(5) track by $index">
                  <span>{{$index+1}}</span></li>
            </ul>
        </div>
    </div>
    
    var app = angular.module('myapp',[]);
    app.controller('ctrlParent',function($scope){
        $scope.myNumber = 5;
        $scope.counter = Array;
    });
    

    See this fiddle for a live example.

    0 讨论(0)
  • 2020-11-22 15:38

    A simpler approach would be (for an example of 5 times):

    <div ng-repeat="x in [].constructor(5) track by $index">
           ...
    </div>
    
    0 讨论(0)
  • I am creating a reusable directive where the max number will come from another ng-repeat. So, this is an edit over the best voted answer.

    Just change the code at controller to this -

    $scope.getNumber = function(num) {
        var temp = [];
        for(var j = 0; j < num; j++){
            temp.push(j)
        }
        return temp; 
    }
    

    This will return a new array with specified number of iterations

    0 讨论(0)
提交回复
热议问题