gpt4 book ai didi

javascript - 在 Angular js 1.6.2 中使用自定义过滤器

转载 作者:行者123 更新时间:2023-12-03 04:51:52 25 4
gpt4 key购买 nike

下面的代码片段与 Angular 1.4.2 完美配合,但是在将 Angular js 库升级到 1.6.2 后。

我搜索了迁移/升级文档..等。没有找到解决办法。

HTML 代码:

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

升级 Angular 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 -->

最佳答案

您需要为您的函数 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;
}
}
});

关于javascript - 在 Angular js 1.6.2 中使用自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42624998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com