选择框在angularjs如何设置默认值 [英] How do I set default value of select box in angularjs

查看:122
本文介绍了选择框在angularjs如何设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于编辑对象的形式,我不能选择选择框的值。

I have a form that is used to edit a object and I can't select a value in the select box.

我有一个JSON阵列重presents的进行编辑,并期待这样的:

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>

选择框填充但对我来说,应该选择在项目的版本相匹配的值。我曾经尝试都 VERSIONID 的versionName ,我甚至尝试设置 NG-选择= 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.

我在这里看着的话,Angularjs网站,并通过无数的教程,但这个仍然有问题一派,不见了。我似乎无法看到的问题是什么,所以任何帮助非常AP preciated

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

增加了的jsfiddle 这里

Added a JsFiddle here

推荐答案

您可以简单的使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆