在角度js 1.6.2中使用自定义过滤器

时间:2017-03-06 11:54:42

标签: javascript angularjs angularjs-ng-repeat angular-filters

下面的片段与angular 1.4.2完美配合,但是在将角度js库升级到1.6.2之后。

我搜索过迁移/升级文档..等等。找不到解决方案。

HTML code:

<input ng-model="query" type="text" name="js_filter" >

我的自定义表的一部分,tbItems.rows将包含一些数组/对象格式的数据

<tr ng-repeat-start="datarow in resultItems = (tbItems.rows | filter:magikFilterQuery ) " >

javascript片段:

myControllers.controller('myContrBasic', ['$scope', '$http',
    function ($scope, $http) {

    $scope.magikFilterQuery = function (item) {
        var term;
        var part;
        if (angular.isUndefined($scope.query) === false) {
          if ($scope.matchcase === true) {
            term = item.data[3];
            part = $scope.query;
          }
          else {
            term = angular.lowercase(item.data[3]);
            part = angular.lowercase($scope.query);
          }
          if (term.indexOf(part) === -1) {
            return false;
          }
          else {
            return true;
          }
        }
        else {
          return true;
        }
      };

升级角度js后出错:

angular.js:14362 Error: [$injector:unpr] http://errors.angularjs.org/1.6.2/$injector/unpr?p0=magikFilterQueryFilterProvider%20%3C-%20magikFilterQueryFilter
    at http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:6:425
    at http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:44:374
    at Object.d [as get] (http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:42:92)
    at http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:44:436
    at Object.d [as get] (http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:42:92)
    at http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:163:474
    at U (http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:125:13)
    at http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:123:123
    at q (http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:7:351)
    at U (http://localhost/site82/folder/js/angular162/angular.min.js?v=8.2.6:123:102) <!-- ngRepeat: datarow in  (tbItems.rows | magikFilterQuery ) as resultItems -->

1 个答案:

答案 0 :(得分:1)

您需要为函数magikFilterQuery定义一个过滤器,如

angular.module('myApp').filter('magikFilterQuery', function() {
  return function(item) {
    var term;
    var part;
    if (angular.isUndefined($scope.query) === false) {
      if ($scope.matchcase === true) {
        term = item.data[3];
        part = $scope.query;
      }
      else {
        term = angular.lowercase(item.data[3]);
        part = angular.lowercase($scope.query);
      }
      if (term.indexOf(part) === -1) {
        return false;
      }
      else {
        return true;
      }
    }
    else {
      return true;
    }
  }
});