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

前端 未结 26 3550
刺人心
刺人心 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:

    sliced list(conditional NG-repeat)

    • {{$index+1}} :: {{ab.name}}

    unsliced list( no conditional NG-repeat)

    • {{$index+1}} :: {{ab.name}}

    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'}];
        }
    

提交回复
热议问题