我有这个代码:
<select ng-init="state" id="state" ng-model="state"
ng-options="state for state in states" required>
</select>
因此,对于默认选择,它将选择控制器中的$scope.state
。这工作正常。但有时,$scope.state
将为空。在这种情况下,我希望默认选择的值为&#39;选择状态&#39;。所以,我尝试了以下内容:
<select ng-init="state || 'Selected a state'" id="state" ng-model="state"
ng-options="state for state in states" required>
</select>
这不起作用。
如何做到这一点?
答案 0 :(得分:1)
您可以像这样添加另一个默认选项:
<select ng-init="state" id="state" ng-model="state" ng-options="state for state in states" required>
<option value="">Select a state</option>
</select>
此 null 选项将被选为默认选项。