AngularJS - 过滤器没有价值
AngularJS - filter has no value
1.这是我的支持文档 $scope:
var supportDoc = this;
......
supportDoc.docs = data;
2。我的 ng-repeat:
data-ng-repeat="doc in supportDoc.docs | $uniqueFilter: doc.icon"
3。我的过滤器:
/*@ngInject */
$uniqueFilter.$inject = [];
ng.module('app.Shared.Layout').filter('$uniqueFilter', $uniqueFilter);
function $uniqueFilter(input, key) {
var unique = {};
var uniqueList = [];
for (var i = 0; i < input.length; i++) {
if (typeof unique[input[i][key]] == "undefined") {
unique[input[i][key]] = "";
uniqueList.push(input[i]);
}
}
return uniqueList;
};
正在达到我的过滤器,但无论我使用什么组合,例如。
| $uniqueFilter: icon
| $uniqueFilter: doc.icon
| $uniqueFilter: supportDoc.docs.icon
等等,我仍然收到同样的错误:
TypeError: 无法读取未定义的 属性 'length'
在 Object.$uniqueFilter (http://localhost:3250/app/dashboards/_shared/layout/filters/unique.client.filter.js:10:34)
在 Object.e [作为调用] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:315)
在 Object.$get (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:34:268)
在 Object.e [作为调用] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:315)
在 http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:38:110
在 Object.d [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:13)
在 $get [as $filter] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:140:92)
在 ib.filter (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:193:186)
在 ib.filter链 (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:193:136)
在 ib.statements (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:192:441)
<!-- ngRepeat: doc in supportDoc.docs | $uniqueFilter: icon
数据在那里。删除过滤器允许显示数据 属性,尽管有重复项,过滤器是用来删除的。
有什么想法吗?提前致谢。
尝试使用 angular.forEach 之类的东西来避免错误。
angular.forEach(input,function(row){
if (typeof unique[row[key]] == "undefined") {
unique[row[key]] = "";
uniqueList.push(row);
}
});
然后试试这个:
ngRepeat: doc in supportDoc.docs | $uniqueFilter: 'icon'
过滤函数应该return另一个函数。
module.filter('$uniqueFilter', function () {
return $uniqueFilter;
})
正如 Vinay K 所提到的那样,您的过滤条件是:
/*@ngInject */
$uniqueFilter.$inject = [];
ng.module('app.Shared.Layout').filter('$uniqueFilter', $uniqueFilter);
function $uniqueFilter() {
return function(input, key) {
var unique = {};
var uniqueList = [];
angular.forEach(input,function(row){
if (typeof unique[row[key]] == "undefined") {
unique[row[key]] = "";
uniqueList.push(row);
}
});
return uniqueList;
}
}
1.这是我的支持文档 $scope:
var supportDoc = this;
......
supportDoc.docs = data;
2。我的 ng-repeat:
data-ng-repeat="doc in supportDoc.docs | $uniqueFilter: doc.icon"
3。我的过滤器:
/*@ngInject */
$uniqueFilter.$inject = [];
ng.module('app.Shared.Layout').filter('$uniqueFilter', $uniqueFilter);
function $uniqueFilter(input, key) {
var unique = {};
var uniqueList = [];
for (var i = 0; i < input.length; i++) {
if (typeof unique[input[i][key]] == "undefined") {
unique[input[i][key]] = "";
uniqueList.push(input[i]);
}
}
return uniqueList;
};
正在达到我的过滤器,但无论我使用什么组合,例如。
| $uniqueFilter: icon
| $uniqueFilter: doc.icon
| $uniqueFilter: supportDoc.docs.icon
等等,我仍然收到同样的错误:
TypeError: 无法读取未定义的 属性 'length' 在 Object.$uniqueFilter (http://localhost:3250/app/dashboards/_shared/layout/filters/unique.client.filter.js:10:34) 在 Object.e [作为调用] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:315) 在 Object.$get (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:34:268) 在 Object.e [作为调用] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:315) 在 http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:38:110 在 Object.d [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:13) 在 $get [as $filter] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:140:92) 在 ib.filter (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:193:186) 在 ib.filter链 (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:193:136) 在 ib.statements (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:192:441)
<!-- ngRepeat: doc in supportDoc.docs | $uniqueFilter: icon
数据在那里。删除过滤器允许显示数据 属性,尽管有重复项,过滤器是用来删除的。
有什么想法吗?提前致谢。
尝试使用 angular.forEach 之类的东西来避免错误。
angular.forEach(input,function(row){
if (typeof unique[row[key]] == "undefined") {
unique[row[key]] = "";
uniqueList.push(row);
}
});
然后试试这个:
ngRepeat: doc in supportDoc.docs | $uniqueFilter: 'icon'
过滤函数应该return另一个函数。
module.filter('$uniqueFilter', function () {
return $uniqueFilter;
})
正如 Vinay K 所提到的那样,您的过滤条件是:
/*@ngInject */
$uniqueFilter.$inject = [];
ng.module('app.Shared.Layout').filter('$uniqueFilter', $uniqueFilter);
function $uniqueFilter() {
return function(input, key) {
var unique = {};
var uniqueList = [];
angular.forEach(input,function(row){
if (typeof unique[row[key]] == "undefined") {
unique[row[key]] = "";
uniqueList.push(row);
}
});
return uniqueList;
}
}