我有以下内容:
$scope.jsonmarkers = [{
"name": "Jeff",
"type": "Organisation + Training Entity",
"userID": "1"
}, {
"name": "Fred",
"type": "Organisation + Training Entity",
"userID": "2"
}];
这是我的html:
<select id="typeselect" multiple ng-options="accnts.type for accnts in jsonmarkers" ng-model="accnt" ng-change="changeaccnt(accnt.type)"></select>
如何匹配该类型,并且仅在ng-options
的每个外观中回显一次?
答案 0 :(得分:7)
要删除重复项,您可以使用AngularUI中的唯一过滤器(此处提供的源代码:AngularUI unique filter),并直接在ng-options(或ng-repeat)中使用。< / p>
请点击此处了解详情: Unique & Stuff
json结构中也存在语法错误。
<强> Working Code 强>
<强> HTML 强>
<div ng-app="app">
<div ng-controller="MainController">
<select id="typeselect" ng-options="accnts.type for accnts in jsonmarkers | unique:'type'" ng-model="accnt" ng-change="changeaccnt(accnt.type)" multiple></select>
</div>
</div>
<强>脚本强>
var app = angular.module("app", []);
app.controller("MainController", function ($scope) {
$scope.jsonmarkers = [{
"name": "Jeff",
"type": "Organisation + Training Entity",
"userID": "1"
}, {
"name": "Fred",
"type": "Organisation + Training Entity",
"userID": "2"
}];
});
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;
};
});
答案 1 :(得分:1)
我修改了NidhishKrishnan给出的答案,以使答案适用于嵌套对象。
例如,如果你有:
$scope.jsonmarkers = [{
"name": "Jeff",
"type": "Organisation + Training Entity",
"userID": "1",
"userData": {
"hire reason":"training"
}
}, {
"name": "Fred",
"type": "Organisation + Training Entity",
"userID": "2"
"userData": {
"hire reason":"training"
}
}];
这是修改后的版本:
angular.module('ArtifactFeederApp.filters', [])
.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)) {
var resolveSearch = function(object, keyString){
if(typeof object == 'undefined'){
return object;
}
var values = keyString.split(".");
var firstValue = values[0];
keyString = keyString.replace(firstValue + ".", "");
if(values.length > 1){
return resolveSearch(object[firstValue], keyString);
} else {
return object[firstValue];
}
}
return resolveSearch(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) {
if(typeof item != 'undefined'){
newItems.push(item);
}
}
});
items = newItems;
}
return items;
};
});
您现在可以:
<div ng-controller="MainController">
<select id="typeselect" ng-options="accnts.type for accnts in jsonmarkers | unique:'userData.hire reason'" ng-model="accnt" ng-change="changeaccnt(accnt.type)" multiple></select>
</div>
您将看到一个仅显示培训作为选项的列表。