Angular.js:控制器中的$ filter [英] Angularjs: $filter in controller

查看:69
本文介绍了Angular.js:控制器中的$ filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使此过滤器无法正常工作.

Having issues with getting this filter to work.

$scope.imgCollection.then(function (images) {
    $scope.images = images.thisGal_images;

    if ($scope.images[0].order == '0') {
        console.log('orgName');
        $scope.images = $filter('orderBy')($scope.images, 'orgName');
    } else {
        console.log('sort order');
        $scope.images = $filter('orderBy')($scope.images, 'sortOrder');
        console.log($scope.images);
    }
});

$ scope.images返回数据库中的图像列表.在初始上传时,由于可以通过ui:sortable对其进行排序,因此sortOrder列将填充为'0'.因此,在初始视图中,我将排序顺序基于文件名.在初始视图之后,将写入DB,并为第一个图像赋予sortOrder 1,并从那里开始递增.

$scope.images returns a list of images from the database. On the initial upload, the sortOrder column is populated with '0' as they can be sorted via ui:sortable. So on the initial view I base the sort order on the file name. After the initial view the DB is written and the first image is given the sortOrder of 1 and increments from there.

这可能是我对$ filter的误解,但$scope.images = $filter('orderBy')($scope.images,'sortOrder');并未基于sortOrder对$ scope.images进行排序.

This could be my misunderstanding of $filter, but $scope.images = $filter('orderBy')($scope.images,'sortOrder'); is not ordering my $scope.images based on sortOrder.

谢谢

推荐答案

我为您创建了一个演示,希望您可以比较代码并找出问题所在.

I created a demo for you and hopefully you can compare the code and figure out the issue.

我想您可能会忘记注入$filter模块.请看一下演示.

I guess you might forget to inject $filter module. Please take a look a the demo.

<div ng-app="myApp" ng-controller="ctrl">
    <div ng-repeat="image in images">{{image}}</div>
    <button ng-click="order('0')">By orgName</button>
    <button ng-click="order('1')">By sortOrder</button>
</div>

var app = angular.module('myApp', []);

function ctrl($scope, $filter) {
    $scope.images = [{
        orgName: 'B',
        sortOrder: 111
    }, {
        orgName: 'A',
        sortOrder: 12
    }, {
        orgName: 'D',
        sortOrder: 13
    }, {
        orgName: 'C',
        sortOrder: 14
    }];

    $scope.order = function (order) {
        if (order == '0') {
            $scope.images = $filter('orderBy')($scope.images, 'orgName');
        } else {
            $scope.images = $filter('orderBy')($scope.images, 'sortOrder');
        }
    }
}

工作演示

这篇关于Angular.js:控制器中的$ filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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