【问题标题】:selected attribute is not working on <option> - ANGULAR所选属性不适用于 <option> - ANGULAR
【发布时间】:2018-06-12 13:56:14
【问题描述】:

我正在使用下拉列表和 select2,我需要编辑产品,当然,所选产品在下拉列表中包含一个所属组/类别,但是当我打开模式时,未选择该组,即使值存在,这是我尝试实现的方法:

1.) 用所有类别填充下拉列表(有效) (*ngFor="let category of mainGroups)

2.) 根据所选文章选择对应的值(不起作用)

<!--category-->
<div class="form-category">
  <label class="control-label dash-control-label col-xs-3">Product category:</label>
  <div class="col-xs-9">
    <select touch-enter-directive [ref]="ref" [nextFocusElement]="articleSubGroup" id="mainGroupSelectEdit" class="form-control dash-form-control select2" style="width: 100%;"
            data-minimum-results-for-search="Infinity" name="mainGroupSelectEdit" required (change)="filterSubById(article.groupId)" [(ngModel)]="article.groupId">
      <option disabled>-/-</option>
      <option [value]="category.id" *ngFor="let category of mainGroups" [selected]="category.id==='a0e25215-a60e-4444-b6ac-4521b7de4b37'">{{category.title}}</option>
    </select>
    {{article.mainGroup.id}} <- here is shown value a0e25215-a60e-4444-b6ac-4521b7de4b37, so I thought that value should be selected in dropdown
  </div>
</div>

你可以看到我用过的人

[selected]="category.id==='a0e25215-a60e-4444-b6ac-4521b7de4b37'"

因为我认为这可能会像这样工作,但不知不觉它不能......

【问题讨论】:

    标签: javascript jquery angular jquery-select2


    【解决方案1】:

    您将选择的模型设置为article.groupId

    只要article.groupId保持不变,你是否使用[selected]都没有关系。

    如果要选择选项 a0e25215-a60e-4444-b6ac-4521b7de4b37,则需要分配 a0e25215-a60e-4444-b6ac-4521b7de4b37 值到article.groupId 属性。在这种情况下,ngModel 设置 selected 属性,只要它的值等于选项之一。

    【讨论】:

    • 代码示例是什么?在您的组件 ts 中,您需要初始化 article.groupId 以获得一个值。它目前有什么价值?
    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 2019-05-06
    • 2019-07-27
    • 2017-10-31
    • 2017-09-04
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    相关资源
    最近更新 更多