-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selected option not selected in ngFor loop #22184
Comments
This absolutely not a bug. You are mixing two things that cannot work together, as they conflict. You can solve your issue by initializing the keyboard variable: |
@wartab How would this solve my issue ? Correct me if I'm wrong but [selected] attribute should contain an expression that returns true or false. Right ? I made some other tests following your answer that made me doubt about some funcitionalities : HTML :
Component :
I am surprised that AZERTY is never default selected unless in last example: |
You should not mix apples and oranges. Why would you use [selected] along with two way databinding? The place for your question is probably Stack Overflow, by the way. |
Indeed I didn't know that SelectControlValueAccessor could do the job. Issue can be closed |
try this: SelectControlValueAccessor |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
When you create a select with ngFor, [selected] tag with an expression does not default select the correct option (and selects nothing at all) :
<select [(ngModel)]="keyboard" name="foo" class="custom-select"> <option *ngFor="let a of foo" [selected]="a.id == 1" [ngValue]="a">{{a.name}}</option> </select>
private foo: any[] = [{id: 1, name: "AZERTY"}, {id: 2, name: "QWERTY"}];
Even when keyboard model is not null, option is not selected
Expected behavior
In above case, select should show "AZERTY".
I also tested with 'keyboard' variable initialized with {id: 1, name: "AZERTY"} but value is not selected.
Tested also with string, object and number comparison expression but it does not work either.
Minimal reproduction of the problem with instructions
see code above
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: