如果我有一个数组
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'red', shade:'dark'},
{name:'yellow', shade:'light'}];
是否可以使用ng-options来构建一个在下拉列表中只有唯一值的select元素,这样red就只会显示一次?
发布于 2013-02-07 18:51:46
发布于 2014-08-04 16:39:08
您可以使用angular.filter模块的uniq/uniq过滤器。
使用: collection | unique: 'property'或collection | unique: 'nested.property'
JS:
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'red', shade:'dark'},
{name:'yellow', shade:'light'}
];HTML:
<select ng-options="color in colors | unique:'name'"></select>发布于 2015-02-12 04:02:05
我没有发现上面的回复是有效的。我做了以下工作:
我的ng-options是这样实现的:
<select ng-model="test" ng-options="cand.candidate for cand in candidates | unique:'candidate'">
<option value="">Select Candidate</option>
</select>将你的模块添加到你的应用中:
var app = angular.module('phonecatApp', ['ui.unique']);使用Bower只安装唯一的Ui Utils。Link to instructions.
https://stackoverflow.com/questions/14748440
复制相似问题