选定的属性不适用于<option> - ANGULAR

时间:2018-06-12 13:56:15

标签: javascript jquery angular jquery-select2

我正在使用下拉菜单和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'"

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

1 个答案:

答案 0 :(得分:0)

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

只要[selected]保持不变,使用article.groupId并不重要。

如果要选择 a0e25215-a60e-4444-b6ac-4521b7de4b37 选项,则需要指定 a0e25215-a60e-4444-b6ac-4521b7de4b37 值到article.groupId属性。在这种情况下,ngModel设置所选属性,只要它的值等于其中一个选项。

相关问题