Angular:ng-repeat orderBy过滤器

时间:2014-01-16 05:37:50

标签: angularjs

鉴于以下内容,我想按displayOrder

订购此列表
angular.module('app').value('sidebarMenus', {
    'Sites': {
        displayName: 'Sites',
        displayOrder: 0
    },
    'Inspections': {
        displayName: 'Inspections',
        displayOrder: 1,
        items: [
            { name: 'Create New', state: 'Admin.UserList' }
        ]
    },
    'Permits': {
        displayName: 'Permits',
        displayOrder: 2
    },
    'Compliance': {
        displayName: 'Compliance',
        displayOrder: 3
    },
    'Accounts': {
        displayName: 'Accounts',
        displayOrder: 4
    },
    'Documents': {
        displayName: 'Documents',
        displayOrder: 5
    },
    'Queries': {
        displayName: 'Queries',
        displayOrder: 6
    },
    'Reports': {
        displayName: 'Reports',
        displayOrder: 7
    },
    'Admin': {
        displayName: 'Admin',
        displayOrder: 8,
        items: [
            { name: 'Users', state: 'Admin.UserList' },
            { name: 'Groups', state: 'Admin.GroupList' },
            { name: 'Workgroups', state: 'na' },
        ]
    }
});

我有这个,但它不起作用。出于某种原因,默认情况下按字母顺序排序。

<li data-ng-repeat="item in sidebar.menus | orderBy: item.displayOrder">
    <a href="javascript:;" data-ng-bind="item.displayName" data-ng-click="sidebar.menu.setMenu(item)"></a>
</li>

结果

Accounts
Admin
Compliance
Documents
Inspections
Permits
Queries
Reports
Sites

1 个答案:

答案 0 :(得分:5)

sidebarMenus是一个对象。 orderBy只能应用于数组。您无法对对象的属性进行排序;)