Angularjs: $filter in controller

后端 未结 1 1512
面向向阳花
面向向阳花 2021-02-05 09:10

Having issues with getting this filter to work.

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

    if ($scope.images[0         


        
相关标签:
1条回答
  • 2021-02-05 09:46

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

    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');
            }
        }
    }
    

    Working Demo

    0 讨论(0)
提交回复
热议问题