在Angular2中设置选定的属性 [英] Set selected attribute in Angular2
本文介绍了在Angular2中设置选定的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有选择组,我想为具有特定ID的对象设置选择的属性.我试图在函数中移动此条件,但无济于事.即使我只是设置[attr.selected]="true"
这也无济于事.
I have select group and i want to set selected attribute for object with certain id. I'm tried to move this condition in function, but it doesn't help. Even if i simply set [attr.selected]="true"
it's also doesn't help.
<div *ngIf="_albums && _albums.length > 0" class="form-group">
<select class="form-control input-lg" id="selectAlbum" [(ngModel)]="_albumId" name="albumId">
<option *ngFor="let album of _albums" [value]="album.id" [attr.selected]="album.id == 0 ? true : null">
{{album.title}}
</option>
</select>
</div>
这很奇怪,因为即使这样也不起作用:
It's weird because even this doesn't work:
<option *ngFor="let album of _albums" [value]="album.id" selected>
{{album.title}}
</option>
推荐答案
我明白了.
将 condition
设置为 value
属性,
set condition
to value
attribute,
[value]="album.id== 0 ? true : null" [attr.selected]="album.id "
演示: https://plnkr.co/edit/BZq43O0PZuAsmyTQLzrD?p=preview
DEMO : https://plnkr.co/edit/BZq43O0PZuAsmyTQLzrD?p=preview
这篇关于在Angular2中设置选定的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文