ng-include how to make a orderBy to an object

2019-08-17 08:40发布

I'm aware that Angular filters can only be applied to arrays, not objects

I'm attempting to include templates added dynamically using the following code. All seems to work well until you see the order

What I would like to have this order:

Create Book Address

here is the Plunker

2条回答
▲ chillily
2楼-- · 2019-08-17 08:56

seems like angular will get ng-include by ordering the names according to their name,

so when you use

$scope.templates = 
{
  _address : 'address.html',
  _create : 'create.html',
  _book : 'book.html'
};

ordering template according to their names, then _address comes first _book comes second _create comes third

simple approach to solve

$scope.templates = 
{
  _a_create : 'create.html',
  _b_address : 'address.html',
  _c_book : 'book.html'
};

here is the Plunker

查看更多
干净又极端
3楼-- · 2019-08-17 09:01

Instead of using a key-value object, why not use an array? ng-repeat orders the iteration by the index of the iterated object/array.

FORKED DEMO

  $scope.templates = [
    'create.html',
    'book.html',
    'address.html'
  ];
查看更多
登录 后发表回答