Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 830
Nick Kahn Avatar asked Dec 13 '14 01:12

Nick Kahn


1 Answers

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

like image 153
JLRishe Avatar answered Sep 23 '22 14:09

JLRishe