Nick Kahn Nick Kahn -4 years ago 190
AngularJS Question

ng-repeat orderBy object

UPDATE:

on my html page I'm using as the following:

<section ng-repeat="template in templates">
<ng-include src="template"></ng-include>
</section>


but the problem is that I need specific file in certain order so is there a way I can control the way order is rendering?

I'm trying to orderby an object how do I do that and I have searched online before posting it here.

function MyCtrl($scope) {
$scope.templates = {
template_address: "../template/address.html",
template_book: "../template/book.html",
template_create: "../template/create.html"
};



<div ng-app ng-controller="MyCtrl">
<ul>
<li ng-repeat="(name, path) in templates">{{name}}: {{path}}</li>
</ul>
</div>


http://jsfiddle.net/abuhamzah/6fbpdm9m/

Answer Source

You can't apply a filter to a plain object, only to arrays.

What you can do is define a method in the controller to convert the object to an array:

$scope.templatesAry = function () {
    var ary = [];
    angular.forEach($scope.templates, function (val, key) {
        ary.push({key: key, val: val});
    });
    return ary;
};

and then filter that:

<li ng-repeat="prop in templatesAry() | orderBy:'-key'">
     {{prop.key}}: {{prop.val}}
</li>

Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download