我想默认选择选择框中的第一个选项。这里的挑战是页面可能有多个选择框,因为它们是动态创建的。
通过查看我的代码,您将了解我想要实现的目标。
HTML:
<form class="form-inline">
<div class="form-group form-group-grid" ng-repeat="fields in selectedTabData.requiredField" ng-switch="fields.paramType">
<label>{{fields.paramName}}<span class="asterisk" ng-if="fields.mandatory==true">*</span></label>
<input type="text" class="form-control" ng-switch-when="Text" ng-model="fields.fieldValue" ng-attr-id="{{fields.paramName}}Id">
<select class="form-control" ng-switch-when="DropDown" ng-options="field.paramKey as field.paramValue for field in fields.paramData" ng-model="fields.fieldValue" ng-attr-id="{{fields.paramName}}Id">
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary form-inline-grid-button" ng-click="getReport()">Run Report</button>
</div>
</form>
所选数据在模型$scope.selectedTabData.requiredField
在控制器中,我将所选值推送到如下变量:
$scope.selectedTabData.requiredField.forEach(function(item)
{
paramValue.push(item.fieldValue);
paramName.push(item.paramName);
})
我尝试过这样做:ng-init="fields.fieldValue = fields.paramData[0]"
<select class="form-control" ng-switch-when="DropDown" ng-options="field.paramKey as field.paramValue for field in fields.paramData" ng-model="fields.fieldValue" ng-init="fields.fieldValue = fields.paramData[0]" ng-attr-id="{{fields.paramName}}Id">
</select>
我不确定它是如何工作的。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
你可以通过ng-repeat inside tag
来实现<select ng-model="fields.fieldValue">
<option ng-repeat="field in fields.paramData" value="{{field.paramKey}}">
{{field.value}}
<option>
</select>
并在控制器中添加此行
$scope.fields.fieldValue = x;
并且在此示例中,x是您想要作为默认选择选项的“field.value”。
答案 1 :(得分:0)
location 8888