如何在angularjs中设置选择框的默认值 [英] How do I set default value of select box in angularjs
问题描述
我有一个用于编辑对象的表单,但我无法在选择框中选择值.
I have a form that is used to edit a object and I can't select a value in the select box.
我有一个表示要编辑的 json 数组,如下所示:
I have a json array which represents the to be edited and look like this:
$scope.item = [{
"objectID": "76",
"versionID": "0",
"versionName": "CURRENT",
"objectName": "xyz",
}]
现在我同时从另一个 json 数组中填充一个选择框,如下所示:
now I am at the same time populating a select box from another json array that looks like this:
$scope.versions = [
{
"id": "0",
"description": "CURRENT",
"name": "CURRENT"
},
{
"id": "114",
"description": "description of Version 2",
"name": "version2"
},
{
"id": "126",
"description": "description of Version 3",
"name": "version3"
},
{
"id": "149",
"description": "description of Version 4",
"name": "version4"
}]
在我的网页中,我正在按如下方式创建选择框:
inside my webpage I am creating the select box as follows:
Version: <select ng-model="item.versionID"
ng-selected="item.versionID"
ng-options="version.name for version in versions"
required>
选择框正在为我填充,但它应该选择与 item
中的版本匹配的值.我已经尝试了 versionID
和 versionName
,我什至尝试过设置 ng-selected="0"
并且这甚至不起作用.
the select box is populating for me but it should be selecting the the value that matches the version in item
. I have tried both versionID
and versionName
, I have even tried setting ng-selected="0"
and that doesn't even work.
我已经在 SO、Angularjs 网站上查看过这里,并在谷歌上搜索并浏览了无数教程,但仍然有问题.我似乎无法看出问题是什么,所以非常感谢任何帮助
I have looked here on SO, the Angularjs site and googled and gone through countless tutorials but still having issues with this. I just can't seem to see what the issue is so any Help greatly appreciated
推荐答案
你可以简单的使用 ng-init 像这样
You can simple use ng-init like this
<select ng-init="somethingHere = options[0]" ng-model="somethingHere" ng-options="option.name for option in options"></select>
这篇关于如何在angularjs中设置选择框的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!