在ng-repeat中,我有一个select下拉列表,如果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
.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包含任何值,我如何将其显示为选中的选项?
发布于 2015-05-14 21:19:59
您可以选择匹配空字符串,由于ng-model中的变量不包含任何内容,因此它将与空值进行匹配,如下所示
<select ng-model="DropdownArray[$index]" ng-options="test.ID as test.Name for test in DropdownList" >
<option value="">Default Value</option>
</select>https://stackoverflow.com/questions/30238358
复制相似问题