Angularjs,填充下拉列表,选择特定值

时间:2015-05-14 13:15:17

标签: angularjs angularjs-ng-repeat

在ng-repeat中,我有一个选择下拉列表,如果obj.Name1中有数据,则必须填入某个选定的值,否则它应该选择选项中的默认第一个值。

<table ng-controller="TestCtrl">

 <tr ng-repeat="obj in MainArray" >
<td>{{$index + 1}}</td>
<td ng-model="MainArray">{{obj.FirstValue}}</td>
<td>

<!-- if obj.Name1 has some value then it has to be selected in select dropdown, else default first value of dropdown must be displayed  -->
<!-- dont know how to put a condition so as to display the obj.Name1 value in the dropdown-->

<select ng-model="DropdownArray[$index]" ng-options="test.ID as test.Name for test in DropdownList" >
     <option>Default Value</option>
 </select>

</table>

//控制器

.controller('TestCtrl',function($scope, $rootScope, $http, URL, $state, $modal, $interval, Notification){



   $scope.DropdownArray=[];

    $scope.DropdownList=[
{"ID":"1","Name":"one"},
{"ID":"2","Name":"two"},
{"ID":"3","Name":"three"}
];

    $scope.MainArray=[
{"FirstValue":"First","Name1":""} ,
{"FirstValue":"Second","Name1":"two"} ,
{"FirstValue":"Third","Name1":"one"},
{"FirstValue":"Forth","Name1":""}
];

})

我的问题是,如果obj.Name1包含任何值,我该如何将其显示为所选选项?

1 个答案:

答案 0 :(得分:0)

您可以创建一个与空字符串匹配的选项,因为ng-model内的变量不包含任何与空值匹配的内容,如下所示

<select ng-model="DropdownArray[$index]" ng-options="test.ID as test.Name for test in DropdownList" >
     <option value="">Default Value</option>
</select>