我在这里看到了Angular select指令的文档:http://docs.angularjs.org/api/ng.directive:select。我不知道如何设置默认值。这是令人困惑的:
select作为数组中值的标签
下面是对象:
{
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
}
html (工作中):
<select><option ng-repeat="value in prop.values">{{value}}</option></select>
然后,我尝试在select元素中添加一个ng-option属性,将prop.value
设置为默认选项(不起作用)。
ng-options="(prop.value) for v in prop.values"
我做错了什么?
发布于 2013-06-27 04:29:24
因此,假设该对象在您的作用域中:
<div ng-controller="MyCtrl">
<select ng-model="prop.value" ng-options="v for v in prop.values">
</select>
</div>
function MyCtrl($scope) {
$scope.prop = {
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
};
}
发布于 2013-12-27 03:55:18
angular documentation for select*没有明确回答这个问题,但它确实存在。如果您查看script.js
,您将看到以下内容:
function MyCntrl($scope) {
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark'},
{name:'yellow', shade:'light'}
];
$scope.color = $scope.colors[2]; // Default the color to red
}
这是html:
<select ng-model="color" ng-options="c.name for c in colors"></select>
这似乎是在使用ng-options
的<select>
上默认选定值的一种更明显的方式。此外,如果您有不同的标签/值,它也会起作用。
*
这是来自Angular 1.2.7的
发布于 2015-06-03 05:22:44
当你从数据库中获取数据,进行修改,然后持久化修改时,这个答案更有用。
<select ng-options="opt.id as opt.name for opt in users" ng-model="selectedUser"></select>
请查看此处的示例:
https://stackoverflow.com/questions/17329495
复制相似问题