如何在angularjs中设置选择框的默认值

时间:2013-08-22 12:48:43

标签: javascript angularjs

我有一个用于编辑对象的表单,我无法在选择框中选择一个值。

我有一个json数组,表示要编辑的内容,如下所示:

       $scope.item = [{
    "objectID": "76",
    "versionID": "0",
    "versionName": "CURRENT",
    "objectName": "xyz",
}]

现在我同时在另一个json数组中填充一个选择框,如下所示:

$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"
}] 

在我的网页内我创建了如下选择框:

Version:<select ng-model="item.versionID" ng-selected="item.versionID" ng-options="version.name for version in versions" required>

选择框正在为我填充,但应该选择与item中的版本匹配的值。我尝试了versionIDversionName,我甚至尝试过设置ng-selected="0",但这甚至无效。

我已经在SO,Angularjs网站上看了一下,并用谷歌搜索并经历了无数的教程,但仍然遇到问题。我似乎无法看到问题是什么,所以任何帮助非常感谢

JSFiddle已添加

添加了JsFiddle here

6 个答案:

答案 0 :(得分:117)

您可以像这样简单地使用ng-init

<select ng-init="somethingHere = options[0]" ng-model="somethingHere" ng-options="option.name for option in options"></select>

答案 1 :(得分:30)

它没有设置默认值,因为你的模型没有绑定到id或name属性,它绑定到每个version对象。尝试将versionID设置为其中一个对象,它应该可以正常工作,即$scope.item.versionID = $scope.versions[2];

如果您想通过id属性进行设置,则需要添加select as语法:

ng-options="version.id as version.name for version in versions"

http://jsfiddle.net/LrhAQ/1/

答案 2 :(得分:29)

你可以追加

track by version.id

到ng-options。

答案 3 :(得分:-1)

搜索并尝试多个非工作选项后,我的选择默认选项正常工作。我找到了一个干净的解决方案: http://www.undefinednull.com/2014/08/11/a-brief-walk-through-of-the-ng-options-in-angularjs/

<select class="ajg-stereo-fader-input-name ajg-select-left"  ng-options="option.name for option in selectOptions" ng-model="inputLeft"></select>
<select class="ajg-stereo-fader-input-name ajg-select-right" ng-options="option.name for option in selectOptions" ng-model="inputRight"></select>

 scope.inputLeft =  scope.selectOptions[0];
 scope.inputRight = scope.selectOptions[1];

答案 4 :(得分:-1)

根据文档select,以下代码对我有用。

public MainWindow()
{
    InitializeComponent();


    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    dispatcherTimer.Tick += dispatcherTimer_Tick; // set it up here

}

 //Executes when the start button is hit, begins timer
private void Button_Click(object sender, RoutedEventArgs e)
{
    dispatcherTimer.Start(); // start timer

}

//Executes when the stop button is hit, ends timers do while loop
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    dispatcherTimer.Stop(); // stop timer
}

答案 5 :(得分:-1)

  <select ng-model="selectedCar" ><option ng-repeat="car in cars "  value="{{car.model}}">{{car.model}}</option></select>
<script>var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) { $scope.cars = [{model : "Ford Mustang", color : "red"}, {model : "Fiat 500", color : "white"},{model : "Volvo XC90", color : "black"}];
$scope.selectedCar=$scope.cars[0].model ;});