ng-选项与ng-init无法正常工作

ng-options with ng-init not working properly

本文关键字:常工作 工作 选项 ng-init ng-      更新时间:2023-09-26

在这种情况下,如果转发器中不存在该值,则此选择不会初始化。是否可以显示与中继器中的值不同的值?

小提琴

<input type="text" ng-model="somethingHere2"/>
Select:
<select ng-init="somethingHere2 = options.default" 
        ng-model="somethingHere2" 
        ng-options="items for items in options.values">
</select>
$scope.workItem = 100;
$scope.options = {
  default:$scope.workItem,
  values:[1,2,3,4,5]
};
$scope.serviceValues = Service; // Service = .factory('Service',...)

可以将单个硬编码<option>添加到<select>,这将是默认选择的选项,通常用于具有"请选择..."或类似 - 注意这必须有一个值=",即一个空字符串:

<input type="text" ng-model="somethingHere2"/>
Select:
<select ng-init="somethingHere2 = options.default" 
        ng-model="somethingHere2" 
        ng-options="items for items in options.values">
    <option value="">Please select...</option>
</select>

这可能允许你做你想做的事 - 除此之外,ng-options 的全部意义在于它迭代提供的值并将选择限制为这些值,因此您不能拥有不在数组中的值。