1

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>
0

2 Answers 2

3

I got it.

set condition to value attribute,

 [value]="album.id== 0 ? true : null" [attr.selected]="album.id "

DEMO : https://plnkr.co/edit/BZq43O0PZuAsmyTQLzrD?p=preview

Sign up to request clarification or add additional context in comments.

8 Comments

i can't, cause value used for ngModel
I'm want default object with id equal to 0. Value used for other purposes(for selecting).
Okay what you want is you want selectedId or defaultId to be zero(0) ???
I want to show object with id equal to zero in dropdown, not just emptiness.
|
0
[attr.selected]="album.id == 0 ? selected : ''"

1 Comment

You don't need the attr prefix - it won't work. Simply [selected]="album.id == 0"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.