基本上,我所拥有的就是这个。
我的剧本:
scope.exampleDetailsForm = {
dropdownItems: ["sample1", "sample2", "sample3", "sample4", "sample5"],
itemSelected: ''
};
我的HTML:
<form name="example" role="form">
<table id="exampleTable">
<tr>
<td class="tableCell">
<label for="LOB">Example:</label>
</td>
<td>
<select ng-model="example"
ng-options="example as example for example in exampleDetailsForm.dropdownItems">
<select>
</td>
</tr>
</table>
</form>
现在,我的itemSelected
会在脚本中进一步向下动态填充我的数据库。所以我要做的是:
如果itemSelected
不为null,我希望select的默认选项为dropDownItems中的对应值。
因此,如果用户登录并选择itemSelected
值为“Sample 4”的内容,那么这就是我想要显示为select语句的默认选项。
答案 0 :(得分:2)
像这样:
scope.exampleDetailsForm = {
dropdownItems: ["sample1", "sample2", "sample3", "sample4", "sample5"],
};
scope.exampleDetailsForm.itemSelected = scope.exampleDetailsForm.dropdownItems[0]
更改ng-model="example"
exampleDetailsForm.itemSelected
我想你想把那个变量用作实际选择的例子。
答案 1 :(得分:0)
尝试将ng-init="example=exampleDetailsForm.itemSelected"
添加到select
元素
答案 2 :(得分:0)
HTML
<form name="example" role="form">
<table id="exampleTable">
<tr>
<td class="tableCell">
<label for="LOB">Example:</label>
</td>
<td>
<select ng-model="example"
ng-init="example=getSelectedItem()"
ng-options="example as example for example in exampleDetailsForm.dropdownItems">
<select>
</td>
</tr>
</table>
</form>
JS
$scope.exampleDetailsForm = {
dropdownItems: ["sample1", "sample2", "sample3", "sample4", "sample5"],
itemSelected: ''
};
$scope.getSelectedItem = function(){
if ($scope.exampleDetailsForm.itemSelected !== ''){
var index = $scope.exampleDetailsForm.dropdownItems.indexOf($scope.exampleDetailsForm.itemSelected);
return $scope.exampleDetailsForm.dropdownItems[index];
}else{
return $scope.exampleDetailsForm.dropdownItems[0];
}
}