Angular2 * ngFor在选择列表中,基于对象中的字符串将其设置为活动状态 [英] Angular2 *ngFor in select list, set active based on string from object
本文介绍了Angular2 * ngFor在选择列表中,基于对象中的字符串将其设置为活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在选择的DropDownList上使用ngFor
.加载了应该在下拉菜单中的选项.
I'm trying using the ngFor
on my select DropDownList. Have loaded in the options which should be in the dropdown.
您在此处看到的代码:
<div class="column small-12 large-2">
<label class="sbw_light">Title:</label><br />
<select [(ngModel)]="passenger.Title">
<option *ngFor="#title of titleArray" [value]="title.Value">{{title.Text}}</option>
</select>
</div>
基于此代码,它会产生一个类似于此图像的下拉列表.
Based on this code, it produces a dropdown which look like this image.
问题是我想基于passenger.Title
将其中一个"Mr or Mrs"设置为活动对象,这是一个字符串"Mr"或"Mrs".
The problem is that I want to set one of them "Mr or Mrs" as the active one based on the passenger.Title
which is a string either "Mr" or "Mrs".
有人可以帮我看看我在做什么错吗?
Any can help see what I'm doing wrong here?
推荐答案
这应该有效
<option *ngFor="let title of titleArray"
[value]="title.Value"
[attr.selected]="passenger.Title==title.Text ? true : null">
{{title.Text}}
</option>
我不确定attr.
部分是否必要.
I'm not sure the attr.
part is necessary.
这篇关于Angular2 * ngFor在选择列表中,基于对象中的字符串将其设置为活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文