【问题标题】:ng-repeat condition filter with two expressions具有两个表达式的 ng-repeat 条件过滤器
【发布时间】:2017-01-27 23:17:18
【问题描述】:

我正在寻找一种方法,在“订单”或“ID”上设置一个 ng-repeat 过滤器。 所以是这样的:

ng-repeat "cd in cds | filter: !order ? orderBy:'id' : orderBy:'order'"

因此,如果订单为空(没有数据),则应按 id 排序,如果订单有数据,则应按“订单”排序。这可能吗?它现在给出两次相同的数据,条件似乎不起作用

【问题讨论】:

标签: angularjs ng-repeat


【解决方案1】:

你很亲密;试试这样的:

ng-repeat "cd in cds | orderBy: (!order ? 'id' : 'order')"

【讨论】:

  • 确实很有趣,谢谢。不知何故没有让 !order 工作必须在控制器中创建一个 $scope 。在答案中告诉你我是如何让它工作的
【解决方案2】:

谢谢你,Andrew Diamond,这对我来说成了 ng-repeat

ng-repeat="cd in cds| orderBy: (ordering != null ? 'order' : 'id')"

这是控制器(必须添加 $scope.ordering):

    function GetCDS() {
        $http({
            method: 'Get',
            url: "/cds"
        })
            .success(function (data, status, headers, config) {        
                $scope.cds = data;
                $scope.ordering = data[0].order;
            })
            .error(function (data, status, headers, config) {
                $scope.message = 'Unexpected Error';
            });
    }

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多