javascript - 为什么在使用 ng-options 时我不能在 select 中有多个 ng-if 选项

标签 javascript angularjs

如果某些表达式为真,我试图在选择框中包含某些选项,并且我发现它添加了第一个选项而不是第二个选项,并且只有当我使用 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/

相关文章:

javascript - 在选择更改 href 和文本

css - Angular.js - 输入两个输入后更改内容?

javascript - 在 AngularJS 的 ng-grid 单元格中检测 Ctrl+Alt+O 按键

javascript - 如何从 xml 响应字符串创建新行

javascript - 何时在 Angularjs 中使用 $q.defer()

javascript - 一起使用 jQuery 和 AngularJS?

javascript - 选择第一个 div 后更改 div 的图标颜色

javascript - 如何在 Storybook React App 中嵌入 StencilJS 组件?

javascript - 引用 JavaScript 插件中的错误

javascript - 为什么 UpdatePanel 会在几分钟后停止工作?