Order by Object key in ng-repeat

后端 未结 3 1037
难免孤独
难免孤独 2020-11-29 08:01

How can I order by key as integer?

I have the following Object;

 $scope.data = {
    \"0\": { data: \"ZERO\" },
    \"1\": { data: \"ONE\" },
    \"2         


        
3条回答
  •  臣服心动
    2020-11-29 08:43

    An option would be use an intermediate filter.

    PLUNK AND Code Snippet

    var app = angular.module('app', []);
    
    app.controller('MainCtrl', function($scope) {
      
      $scope.template = {
        "0": { data: "ZERO" },
        "1": { data: "ONE" },
        "2": { data: "TWO"  },
        "3": { data: "TREE" },
        "5": { data: "FIVE" },
        "6": { data: "SIX" },
        "10":{ data:  "TEN" },
        "11": { data: "ELEVEN" },
        "12": { data: "TWELVE" },
        "13": { data: "THIRTEEN" },
        "20": { data: "TWENTY"}
      }
     
    });
    
    app.filter('toArray', function() { return function(obj) {
        if (!(obj instanceof Object)) return obj;
        return _.map(obj, function(val, key) {
            return Object.defineProperty(val, '$key', {__proto__: null, value: key});
        });
    }});
    
    
    
    
        
    {{key}} : {{value.$key}} : {{value.data}}

    NOTE:

    The above filter requires Underscore.js, if you don't use it, can rewrite the filter.

提交回复
热议问题