AngularJS-ng选项在使用唯一筛选器后不返回所有匹配数据
我有一组简单的ng选项。每个子ng选项都应该根据填充的父项缩小其数据范围 然而,为了重复主要的父项——“国家”,我引入了独特的过滤器,因为它是重复项目,例如,没有过滤器,英国在下拉列表中出现了两次 然而,在选择英国之后,我希望看到伦敦和利兹在城市中的排名下降,但只有伦敦出现 HTML:AngularJS-ng选项在使用唯一筛选器后不返回所有匹配数据,angularjs,angularjs-scope,ng-options,angular-filters,Angularjs,Angularjs Scope,Ng Options,Angular Filters,我有一组简单的ng选项。每个子ng选项都应该根据填充的父项缩小其数据范围 然而,为了重复主要的父项——“国家”,我引入了独特的过滤器,因为它是重复项目,例如,没有过滤器,英国在下拉列表中出现了两次 然而,在选择英国之后,我希望看到伦敦和利兹在城市中的排名下降,但只有伦敦出现 HTML: 国家 --国家-- 城市 --城市-- 工作类别 --工作类别-- JS: app.controller('MainCtrl',函数($scope){ $scope.name='World'; $scope
国家
--国家--
城市
--城市--
工作类别
--工作类别--
JS:
app.controller('MainCtrl',函数($scope){
$scope.name='World';
$scope.Data=[{
“国家”:“英国”,
“城市”:“伦敦”,
“部门”:“财务”,
“等级”:“2”
}, {
“国家”:“英国”,
“城市”:“利兹”,
“部门”:“IT”,
“等级”:“3”
}, {
“国家”:“美国”,
“城市”:“德克萨斯州”,
“部门”:“风险”,
“等级”:“5”
}];
});
app.filter('unique',function(){
返回函数(项目、过滤器){
if(filterOn==false){
退货项目;
}
如果((filterOn | | angular.isUndefined(filterOn))&angular.isArray(项目)){
var hashCheck={},newItems=[];
var extractValueToCompare=函数(项){
if(角度等高线(项目)和角度等高线(过滤器)){
返回项目[过滤器];
}否则{
退货项目;
}
};
角度。forEach(项目、功能(项目){
var值检查,isDuplicate=false;
对于(var i=0;i
这是我的建议:问题不在于唯一筛选器,而在于$scope.country中的值。它不是包含国家的字符串,而是包含国家的对象
{
"Country": "UK",
"City": "London",
"Department": "Finance",
"rank": "2"
}
如果您将城市上的过滤器更改为“country.country”,则它将工作
更好的做法是将表达式“country as country.country for country in Data”中的“unique:”重命名为“item as country.country for country in Data”中的“unique:”并在城市过滤器中使用item.country。谢谢!我把country.country放在过滤器中,这对城市有效。。。但随后尝试了类别过滤器中的city.city,但失败了:数据不包含任何类别。
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.Data = [{
"Country": "UK",
"City": "London",
"Department": "Finance",
"rank": "2"
}, {
"Country": "UK",
"City": "Leeds",
"Department": "IT",
"rank": "3"
}, {
"Country": "USA",
"City": "Texas",
"Department": "Risk",
"rank": "5"
}];
});
app.filter('unique', function() {
return function(items, filterOn) {
if (filterOn === false) {
return items;
}
if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) {
var hashCheck = {}, newItems = [];
var extractValueToCompare = function(item) {
if (angular.isObject(item) && angular.isString(filterOn)) {
return item[filterOn];
} else {
return item;
}
};
angular.forEach(items, function(item) {
var valueToCheck, isDuplicate = false;
for (var i = 0; i < newItems.length; i++) {
if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
newItems.push(item);
}
});
items = newItems;
}
return items;
};
});
{
"Country": "UK",
"City": "London",
"Department": "Finance",
"rank": "2"
}