Uak Uak - 3 months ago 43
HTML Question

Angular 2 - [attr.selected] works but select nothing

I have a simple problem but i don't find a solution

I have this select:

<select name="projectObjectUpdate" [ngFormControl]="_projectAmountForm.controls['projectObject']"
class="form-control form-control-select2-field">
<option *ngFor="let object of _projectObjectList" [ngValue]="object" [attr.selected]=" === _projectAmountForm.controls['projectObject'] ? true : null">

In the HTML code, good option is selected

but nothing is selected on screen

anyone knows the problem ?


You also could just bind ngModel on your select element.

<select name="projectObjectUpdate" [ngModel]="_projectAmountForm.controls['projectObject']" [ngFormControl]="_projectAmountForm.controls['projectObject']"
        class="form-control form-control-select2-field" >
    <option *ngFor="let object of _projectObjectList" [ngValue]="object" >

This will always select the option element with the same value as _projectAmountForm.controls['projectObject']


Sorry, yes ngValue was wrong! I meant ngModel. Corrected.