我想要从ng-options中过滤唯一值。我得到的只是重复的条目,如果我是模型,我会得到一大堆空行。我创造了一个插件。感谢您的帮助。
提前致谢
http://plnkr.co/edit/UCI6mVHjEiyZTYUpQ18Y?p=preview
<select ng-options="c.metaInfo.subType for c in myArray">
答案 0 :(得分:1)
这是工作Plunker
你的代码有几个问题:首先myArray
不是一个对象或一个数组,它是一个错误的语法,而是检查我的。
然后要使用ng-options
,您还需要设置ng-model
。
JS:
var myApp = angular.module('myApp', []);
myApp.controller("Test", function($scope) {
$scope.myArray = [{
"type": "application/doc",
"url": "www.google.com",
"metaInfo": {
"subType": "doc",
"filesize": "60kb"
}
}, {
"type": "application/pdf",
"metaInfo": {
"subType": "pdf",
"filesize": "60kb"
}
}, {
"type": "application/xls",
"metaInfo": {
"subType": "xls",
"filesize": "60kb"
}
}, {
"type": "application/pdf",
"metaInfo": {
"subType": "pdf",
"filesize": "60kb"
}
}];
});
HTML:
<body ng-controller="Test">
<select ng-options="c.metaInfo.subType for c in myArray | unique:'metaInfo.subType'" ng-model="selected">
<option value="">All</option>
</select>
<p>Selected: {{selected || "None"}}</p>
</body>
要删除重复项,您需要使用像this one这样的Angular模块来使用过滤器unique
。
我用所需的一切更新了Plunker。
答案 1 :(得分:1)
您需要像这样更改您的HTML代码
<select ng-model="subType" ng-options="c.metaInfo.subType for c in myArray">
<option value="">All</option>
</select>
并且需要遵循数组语法。使用以下代码:
$scope.myArray = [
{
"type": "application/doc",
"url": "www.google.com",
"metaInfo": {
"subType": "doc",
"filesize": "60kb"
},
},
{
"type": "application/pdf",
"metaInfo": {
"subType": "pdf",
"filesize": "60kb"
},
},
{
"type": "application/xls",
"metaInfo": {
"subType": "xls",
"filesize": "60kb"
},
},
{
"type": "application/pdf",
"metaInfo": {
"subType": "pdf",
"filesize": "60kb"
},
}
];