简体   繁体   English

无法在选项中选择值(角度下拉列表。)

[英]Unable to select value in option (Angular dropdown list.)

I have searched far and wide.我已经搜索了很远很远。 What I got from other answers is that I need to use [selected] if I want to select a particular option.我从其他答案中得到的是,如果我想选择特定选项,我需要使用[selected]

It does not seem to be working.它似乎不起作用。 Why?为什么? Here is what I have:这是我所拥有的:

<ngx-datatable-column name="status" prop="operationStatus" [flexGrow]="1">
    <ng-template let-value="value2" let-row="row" let-rowIndex="rowIndex" gx-datatable-cell-template>
          <select class="geraeteStatus" [(ngModel)]="selectableGerateStatus">
             <option *ngFor="let e of selectableGerateStatus" [ngValue]="e" 
                 [selected]="e.id === value2">
                {{e.id}}
             </option>
          </select>
   </ng-template>
</ngx-datatable-column>

I am sure that the value of e.id equals that of value2 at least once per iteration.我确信每次迭代至少一次e.id的值等于value2 However it just does not work.但是,它不起作用。 Actually I can put whatever I wish (e.id === 2, or even 'true') into [selected] it does not have any effect.其实我可以把我想要的任何东西(e.id === 2,甚至'true')放入[selected]它没有任何效果。 I have no idea what I am missing.我不知道我错过了什么。 Any answer is highly appreciated.任何答案都受到高度赞赏。

There is no need to use [selected] in your options 无需在选项中使用[selected]

just do some changes like below 像下面做一些更改

Component.html Component.html

<select class="geraeteStatus" (change)="onChange($event)" [(ngModel)]="selectedStatus">
   <option *ngFor="let e of selectableGerateStatus" [ngValue]="e.id">
     {{e.id}}
   </option>
</select>

but here you need to match ngValue with your ngModel then only it will be selected. 但是在这里,您需要将ngValuengModel匹配,然后才将其选中。

Component.ts Component.ts

selectedStatus: string;

onChange(status: string) {
   this.selectedStatus = status;
}

For more info about select options check here 有关选择选项的更多信息,请单击此处

You need to set the field that should be bound to this 'select' into ngModel instead of the list of possible options. 您需要将应绑定到“ select”的字段设置为ngModel,而不是可能的选项列表。 Please change your code like this: 请像这样更改您的代码:

<select class="geraeteStatus" [(ngModel)]="x.status">
         <option *ngFor="let e of selectableGerateStatus" [ngValue]="e">
            {{e.id}}
         </option>
</select>

' x.status ' has to be replaced by your real field. x.status ”必须替换为您的真实字段。

Bind the option with value instead of ngValue as below. 将选项与value而不是ngValue绑定,如下所示。

<select class="my-status" [(ngModel)]="myModel.status">
         <option [ngValue]="null" selected disabled> Select Status </option>
         <option *ngFor="let e of selectableGerateStatus" [value]="e">
            {{e.id}}
         </option>
</select>

I present how I solved the problem. 我介绍如何解决这个问题。 Note that I am quite new to angular, and my approach might be rudimentary. 请注意,我是新手,对角度而言,我的方法可能是基本的。 I also want to give credit to @Eliseo who highlighted the fact that ngModel and selected must not be used together . 我还要感谢@Eliseo,他强调了ngModel和selected不能一起使用的事实。 That's whay I got rid of the former and used selected only: 这就是为什么我摆脱了前者,只使用了选择的东西:

component.html component.html

 <select class="geraeteStatus" (change)="changeGaerateStatus($event, rowIndex)">
          <option *ngFor="let e of selectableGerateStatus" [selected]="value == e.id">
               {{e.label}}
            </option>
 </select>

and the .ts file: 和.ts文件:

    this.selectableGerateStatus = [
  {
    id: 0,
    label: BaseDataComponent.NEW
  }, {
    id: 1,
    label: BaseDataComponent.ACTIVE
  }, {
    id: 2,
    label: BaseDataComponent.DEINSTALLED
  }
]

It solved my problem, but it might not be the best approach. 它解决了我的问题,但这可能不是最佳方法。 Feel free to comment. 随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM