$ filter未在指令函数

时间:2017-02-10 21:43:54

标签: angularjs angularjs-directive

我刚刚回到Angular,我很茫然。我正在使用我在之前的项目中使用过的datagrid指令,而我所要做的就是让它显示简单的数据。

这是我使用它的模块调用:

define([
  'angular',
  'components/home/home-controller',
  '../../../js/px-datagrid'
], function (angular, homeController, pxDatagridDirective) {
    'use strict';
    var homeModule = angular.module('app.home', ['ui.grid']);
    homeModule.controller('home', ['$scope','$timeout','$log','$http','$filter',homeController]);
    homeModule.directive('pxDatagrid', [ '$log','$filter',pxDatagridDirective ]);
});

以下是px-datagrid.js的关键部分:

define(['angular', 'text!./px-datagrid.tmpl', 'angular-ui-bootstrap',
       'angular-sanitize'],
        function(angular, tmpl, angularBootstrap) {
            return function($log, $filter){
                var PxDatagrid = {
                    ...
                    applyFilters: function($scope, $log, $filter){
                        if($scope.tableData && ($scope.tableData.$resolved || $scope.tableData.length > 0)){
                            var filtered = $filter('filter')($scope.tableData, $scope.searchText);
                            filtered = $filter('orderBy')(filtered, $scope.sortBy, $scope.sortOrder === 'desc');
                            //removes dependency on startFrom filter
                            filtered = filtered.slice( ($scope.currentPage - 1) * $scope.itemsPerPage );
                            $scope.filtered = $filter('limitTo')(filtered, $scope.itemsPerPage);
                        } else {
                            $scope.filtered = null;
                        }
                    }
                    ...
                }
           }   
      }

无论我做什么,我都会在applyFilters中收到以下错误:

  

TypeError:$ filter不是函数

这似乎具有相同的依赖注入,相同的结构,我以前可以找到的所有内容(它工作的地方)。我能找到的所有信息只是说注入它并且它有效......必须有一些简单的东西我不知道。

1 个答案:

答案 0 :(得分:0)

试试这个

define(['angular', 'text!./px-datagrid.tmpl', 'angular-ui-bootstrap',
       'angular-sanitize','$filter'],
        function(angular, tmpl, angularBootstrapm, sanitize, $filter ) {
            return function($log, $filter){
                var PxDatagrid = {
                    ...
                    applyFilters: function($scope, $log){
                        if($scope.tableData && ($scope.tableData.$resolved || $scope.tableData.length > 0)){
                            var filtered = $filter('filter')($scope.tableData, $scope.searchText);
                            filtered = $filter('orderBy')(filtered, $scope.sortBy, $scope.sortOrder === 'desc');
                            //removes dependency on startFrom filter
                            filtered = filtered.slice( ($scope.currentPage - 1) * $scope.itemsPerPage );
                            $scope.filtered = $filter('limitTo')(filtered, $scope.itemsPerPage);
                        } else {
                            $scope.filtered = null;
                        }
                    }
                    ...
                }
           }   
      }