简体   繁体   中英

angularjs limitTo filter not working with odd json array

I have an odd json array that I can ng-repeat just fine, but I'm having trouble limiting this list with the limitTo filter. I'm getting the json object with an http call so I can't change how the object is formed on the back-end.

HTML:

<div ng-controller="myCtrl">
    <div ng-repeat="school in schoolList | limitTo:2">
        {{school.school_name}}
    </div>
</div>

JSON:

{"0":{"school_name":"FAKE ELEMENTARY"},
 "1":{"school_name":"CENTRAL HIGH"},
 "2":{"school_name":"OAK RIDGE ELEMENTARY"},
 "3":{"school_name":"PINE MEADOW ELEMENTARY"},
 "4":{"school_name":"AMERICAN MIDDLE"},
 "5":{"school_name":"BIG SENIOR HIGH"},
 "6":{"school_name":"ST FRANCIS ELEMENTARY SCHOOL"}

Here is the jsfiddle

My first thought is that I could somehow use $index but I haven't had any luck thus far. Any help would be appreciated. I apologize in advanced if this has already been answered but I've been searching for a few days without finding anything helpful. Thanks.

From the angular source the first check is:

if (!isArray(input) && !isString(input)) return input;

So you will have to convert that data to an array, see working fiddle here: http://jsfiddle.net/Nu7rZ/103/

angular source link: https://github.com/angular/angular.js/blob/master/src/ng/filter/limitTo.js#L3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM