ng-repeat orderBy 对象 [英] ng-repeat orderBy object

查看:25
本文介绍了ng-repeat orderBy 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

在我的 html 页面上,我使用如下:

<ng-include src="模板"></ng-include></节>

但问题是我需要按特定顺序排列特定文件,所以有没有办法控制顺序呈现的方式?

我正在尝试通过一个对象来订购我该怎么做,并且在将其发布到此处之前我已经在网上进行了搜索.

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>

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

解决方案

不能对普通对象应用过滤器,only到数组.

你可以做的是在控制器中定义一个方法来将对象转换为数组:

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

然后过滤:

  • {{prop.key}}:{{prop.val}}
  • 示例

    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/

    解决方案

    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

    这篇关于ng-repeat orderBy 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆