如果某些表达式为真,我试图在选择框中包含某些选项,并且我发现它添加了第一个选项而不是第二个选项,并且只有当我使用 ng-options 时才会发生这种情况
我确实尝试使用更复杂的表达式,但在删除 ng-options
和 if-ng 后,将其简化为 ng-if="true"
来验证我的怀疑添加了选项。
这是我的用户模型
$scope.users = [{
"id": "123",
"description": "Tester",
"name": "Jane"
},
{
"id": "312",
"description": "Dev",
"name": "John"
}];
这是我的简化选择
<select class="fullWidth" ng-model="name_model" ng-selected="name_model"
ng-options="user.name as user.description for user in users" required>
<option ng-if="true" value="">-- Please Select an Approver Type: --</option>
<option ng-if="true" value="">-- Please Select One: --</option>
</select>
这是一个错误,是不允许的还是我忽略了某些东西。我有一个JSFiddle这可能比我在这里更好地解释
最佳答案
基本上对于 ng-if = true
的 2 个选项,代码实际上相当于
<option value=""> Please Select an Approver Type: </option>
<option value=""> Please Select One: </option>
自 ngOptions仅接受一个硬编码选项,
Optionally, a single hard-coded element, with the value set to an empty string, can be nested into the element. This element will then represent null or "not selected" option.
第一个将被覆盖。
关于javascript - 为什么在使用 ng-options 时我不能在 select 中有多个 ng-if 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512419/