如何使用NG-选项来设置选择元素的默认值 [英] how to use ng-option to set default value of select element
问题描述
我见过的角度选择指令这里的文档:<一href=\"http://docs.angularjs.org/api/ng.directive:select\">http://docs.angularjs.org/api/ng.directive:select.
我想不出如何设置默认值。这是困惑我:
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 me:
选择为标签的数组值
下面是对象:
{
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
}
的html格式的(工作):
The html (working):
<select><option ng-repeat="value in prop.values">{{value}}</option></select>
然后我尝试添加选择元素内的NG选项属性设置 prop.value
作为默认选项(不工作)。
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>
&NBSP;
function MyCtrl($scope) {
$scope.prop = {
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
};
}
工作Plunkr: http://plnkr.co/edit/wTRXZYEPrZJRizEltQ2g
这篇关于如何使用NG-选项来设置选择元素的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!