gpt4 book ai didi

Angular 2 : How to handle *ngif in option select

转载 作者:太空狗 更新时间:2023-10-29 18:03:53 27 4
gpt4 key购买 nike

如何使用 *ngif 设置特定的默认选择选项?

我需要从数据库中重新加载数据进行编辑,当我检索数据时,我需要在存储值的基础上设置选项的默认值。

我有这个:

<select class="form-control" id="deviceModel">
<option value="">Select a category</option>
<option *ngFor='let element of category'*ngIf="{{element}}==={{nameDevice}}" value="{{element}}">{{element}}</option>
</select>

提前致谢。

最佳答案

ngIf 用于结构操作,基本上是在 DOM 中或已删除的内容。如果你想绑定(bind)到选定的属性,你可以使用:

<select class="form-control" id="deviceModel">
<option value="">Select a category</option>
<option *ngFor='let element of category' [selected]="element === nameDevice" [value]="element">{{element}}</option>
</select>

来自 https://angular.io/docs/ts/latest/api/forms/index/SelectControlValueAccessor-directive.html :

If your option values are simple strings, you can bind to the normal value property on the option. If your option values happen to be objects (and you'd like to save the selection in your form as an object), use ngValue instead.

如果你想使用 ngModel 绑定(bind),看看 Binding select element to object in Angular 2

请注意,在使用属性绑定(bind)时,您不要/不应该使用字符串插值

关于 Angular 2 : How to handle *ngif in option select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40797296/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com