vim*_*984 3 javascript angularjs
我有以下内容:
$scope.jsonmarkers = [{
"name": "Jeff",
"type": "Organisation + Training Entity",
"userID": "1"
}, {
"name": "Fred",
"type": "Organisation + Training Entity",
"userID": "2"
}];
Run Code Online (Sandbox Code Playgroud)
这在我的HTML中:
<select id="typeselect" multiple ng-options="accnts.type for accnts in jsonmarkers" ng-model="accnt" ng-change="changeaccnt(accnt.type)"></select>
Run Code Online (Sandbox Code Playgroud)
我如何进行匹配类型,并且只在每次出现时回显一次ng-options?
为了删除重复项,您可以使用AngularUI中的唯一过滤器(此处提供的源代码:AngularUI唯一过滤器)并直接在ng-options(或ng-repeat)中使用它.
在这里寻找更多:独特和东西
此外,json结构中存在语法错误.
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>
Run Code Online (Sandbox Code Playgroud)
脚本
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;
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10661 次 |
| 最近记录: |