Skip to content
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

Closed
biologeek opened this issue Feb 12, 2018 · 6 comments
Closed

Selected option not selected in ngFor loop #22184

biologeek opened this issue Feb 12, 2018 · 6 comments

Comments

@biologeek
Copy link

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

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


Angular version: 5.0.0

Browser:
- [ x ] Chrome (desktop) version 64.0.3282.140
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ x ] Firefox version 52.6
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: 8.9.4
- Platform:  Debian

@wartab
Copy link
Contributor

wartab commented Feb 13, 2018

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:
this.keyboard = this.foo.find(keyboard => keyboard.id === 1);

@biologeek
Copy link
Author

biologeek commented Feb 13, 2018

@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 :

<form style="margin : 20%;">
	<div class="row">
		<div class="form-group">
			<label for="foo">K1 :{{keyboard | json}}</label>
			<select [(ngModel)]="keyboard" name="foo" class="custom-select"> 
				<option *ngFor="let a of foo" [ngValue]="a">{{a.name}}</option> 
			</select>
		</div>
	</div>
	<div class="row">


		<div class="form-group">
			<label for="fooz">K2 :{{keyboard2 | json}}</label>
			<select [(ngModel)]="keyboard2" name="fooz" class="custom-select"> 
				<option [ngValue]="foo[0]">AZERTY</option> 
				<option [ngValue]="foo[1]">QWERTY</option> 
			</select>
		</div>
	</div>
	<div class="row">


		<div class="form-group">
			<label for="fooa">K2 :{{keyboardStr | json}}</label>
			<select [(ngModel)]="keyboardStr" name="fooa" class="custom-select"> 
				<option [selected]="keyboardStr == 'AZERTY'" [ngValue]="AZERTY">AZERTY</option> 
				<option [selected]="keyboardStr == 'QWERTY'" [ngValue]="QWERTY">QWERTY</option> 
			</select>
		</div>
	</div>
	<div class="row">

		<div class="form-group">
			<label for="fooa">K2-bis :{{keyboardStr | json}}</label>
			<select [(ngModel)]="keyboardStr" name="fooa" class="custom-select"> 
				<option [selected]="keyboardStr == 'AZERTY'" [value]="AZERTY">AZERTY</option> 
				<option [selected]="keyboardStr == 'QWERTY'" [value]="QWERTY">QWERTY</option> 
			</select>
		</div>
	</div>
	<div class="row">

		<div class="form-group">
			<label for="fooa">K2-ter :{{keyboardStr | json}}</label>
			<select [(ngModel)]="keyboardStr" name="fooa" class="custom-select"> 
				<option [selected]="keyboardStr == 'AZERTY'" value="AZERTY">AZERTY</option> 
				<option [selected]="keyboardStr == 'QWERTY'" value="QWERTY">QWERTY</option> 
			</select>
		</div>
	</div>
</form>	

Component :

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

	private foo: any[] = [{id: 1, name: "AZERTY"}, {id: 2, name: "QWERTY"}];
	private keyboard: any = {id: 1, name: 'AZERTY'};
	private keyboard2 : any = {id: 1, name: 'AZERTY'};
	private keyboardStr : string = 'AZERTY';

  constructor() { }

  ngOnInit() {
  }

}

I am surprised that AZERTY is never default selected unless in last example:

untitled

@wartab
Copy link
Contributor

wartab commented Feb 13, 2018

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.

@biologeek
Copy link
Author

Indeed I didn't know that SelectControlValueAccessor could do the job. Issue can be closed

@jiangshanwe
Copy link

try this: SelectControlValueAccessor

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants