如何使用 ng-option 设置选择元素的默认值 [英] how to use ng-option to set default value of select element
问题描述
我在这里看到了 Angular select 指令的文档:http://docs.angularjs.org/api/ng.directive:选择.我无法弄清楚如何设置默认值.这令人困惑:
<块引用>选择作为数组中值的标签
这里是对象:
<代码>{"类型": "选择","name": "服务","value": "服务 3",值":[服务 1"、服务 2"、服务 3"、服务 4"]}
html(工作):
然后我尝试在 select 元素中添加一个 ng-option 属性以将 prop.value
设置为默认选项(不起作用).
ng-options="(prop.value) for v in prop.values"
我做错了什么?
因此假设该对象在您的范围内:
<select ng-model="prop.value" ng-options="v for v in prop.values"></选择>
function MyCtrl($scope) {$scope.prop = {"类型": "选择","name": "服务","value": "服务 3",值":[服务 1"、服务 2"、服务 3"、服务 4"]};}
工作 Plunkr:http://plnkr.co/edit/wTRXZYEPrZJRizEltQ2g
I've seen the documentation of the Angular select directive here: http://docs.angularjs.org/api/ng.directive:select. I can't figure how to set the default value. This is confusing:
select as label for value in array
Here is the object:
{
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
}
The html (working):
<select><option ng-repeat="value in prop.values">{{value}}</option></select>
and then I'm trying to add an ng-option attribute inside the select element to set prop.value
as the default option (not working).
ng-options="(prop.value) for v in prop.values"
What am i doing wrong?
So assuming that object is in your scope:
<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"]
};
}
Working Plunkr: http://plnkr.co/edit/wTRXZYEPrZJRizEltQ2g
这篇关于如何使用 ng-option 设置选择元素的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!