我在这里创建下拉菜单并设置默认值。这是我的对象
$scope.currentprop={
name : "graphFillDirection",
members : [{
name : "ttb",
caption : "Top to Bottom",
}, {
name : "btt",
caption : "Bottom to Top"
}, {
name : "ltr",
caption : "Left to Right"
},{
name : "rtl",
caption : "Right to Left"
}
],
value : "ttb",
//suppose it is : value:""
}
在这个对象中,我有members
数组和属性value
。所以检查迭代 members.name
和 value
属性,这是我的 html。
<select ng-model="currentprop.value" ng-options="value.name as value.name for (key,value) in currentprop.members" ng-selected="{{currentprop.value == value.name}}"></select>
如果大小写 currentprop.value == value.name
匹配,那么我将值设置为 ng-model。我的问题是,如果 currentprop.value 类似于 value:""
,那么迭代值永远不会与 currentprop.value
匹配。所以我需要的是如果 value:""
那么我将 members[0].name
设置为 ng-model。有些请帮助我。 plnkr这里
最佳答案
在 select
中尝试 ng-init
:
<select ng-model="currentprop.value"
ng-init="currentprop.value = currentprop.value || currentprop.members[0].name"
ng-options="value.name as value.name for (key,value) in currentprop.members"
ng-selected="{{currentprop.value == value.name}}">
</select>
关于javascript - 使用条件angularjs设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615115/