我正在创建一个选择下拉列表,该下拉列表将填充对象列表的现有属性值(唯一)。这将用作过滤器。
例如,假设我的列表看起来像这样:
[
{
category: "A"
},
{
category: "B"
},
{
category: "C"
},
{
category: "A"
}
]
我希望我的下拉列表中包含A,B和C的值。我可以遍历列表和类别属性并创建一个唯一的列表,但这看起来很混乱。 Angular中有什么可以做到的吗?
答案 0 :(得分:1)
这可能就是你要找的东西:
Making a select list in AngularJS
app.js:
var app = angular.module('plunker', ['ui.utils']);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.vals = [
{
category: "A"
},
{
category: "B"
},
{
category: "C"
},
{
category: "A"
}
]
});
的index.html
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js" data-semver="1.2.19"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-utils/0.1.1/angular-ui-utils.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<select ng-model='selvalue' ng-options='val.category for val in vals | unique:category'></select>
Selected:{{selvalue.category}}
</body>
</html>