Repository navigation
Closed
Labels
P4A relatively minor issue that is not relevant to core functionsarea: material/form-fieldarea: material/select
Description
Bug
when using a select component inside a reactive form, and updating the selection using the select component, the corresponding formControl is not being updated,
say I have a component that is inside formGroup:
html:
<form [formGroup]="formGroup">
<mat-form-field>
<mat-select placeholder="Pick item..." formControlName="selectedItem">
<mat-option *ngFor="let item of items">
{{item.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<button (click)="changeValue()">Change value</button>
component:
{
@ViewChild(MatSelect)selectComponent:MatSelect;
ngOnInit(){
this.formGroup = this.formBuilder.group({
selectedItem: ['', Validators.required],
});
this.items = [{name:'a'},{name:'b'},{name:'c'}];
}
changeValue(){
this.selectComponent.value = this.items[1];
}
}
when I press the "Change value" button, the selectComponent.value is being set,
and the changes do reflect on the ui, but the corresponding formControl(selectedItem) is not being set
and the form is invalid (the field is required)
What is the expected behavior?
the formControl should be updated with the value being set on the select component
What is the current behavior?
the formControl is not being updated
What are the steps to reproduce?
Providing a StackBlitz reproduction is the best way to share your issue.
StackBlitz starter: https://goo.gl/wwnhMV
What is the use-case or motivation for changing an existing behavior?
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular CLI: 1.7.2
Node: 8.9.1
OS: win32 x64
Angular: 5.2.7
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cdk: 5.2.3
@angular/cli: 1.7.2
@angular/material-moment-adapter: 5.2.3
@angular/material: 5.2.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.1
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0
Is there anything else we should know?
Reacted by Emre Celenli, Iryna Babrouskaya, Asham Sabir, Victor Carvalho, Maxime, Jason Yeomans, dardolanda, Phil Zuschlag, Raymond , Musa and 11 more
Metadata
Metadata
Assignees
Labels
P4A relatively minor issue that is not relevant to core functionsarea: material/form-fieldarea: material/select
Activity
julianobrasil commentedon Mar 1, 2018
As the options are objects, it's normal that the comparison fails when you select programmatically the value. You need to set a
compareWithfunction (take a look at #6970 (comment)):avindor commentedon Mar 2, 2018
First of all, I'm using the same object when selecting - I have a list of items and I'm selecting one of them, so there should be no issue with comparison.
In my original code, I am using a compareWith input, but still having the same behavior, here is a more complete example that uses compareWith and is not working:
when I click the "selectOption" button, the ui changes, but the form control (reflected in the ui with the following html:
<p class="col-auto selected-food"> Selected value: {{formGroup.get('favoriteFood').value | json}} </p>is not changingjulianobrasil commentedon Mar 2, 2018
I completely missed that part, sorry.
julianobrasil commentedon Mar 2, 2018
Aparently this is causing this behavior:
https://github.com/angular/material2/blob/915a2b732760eca047939acff7b79c00f7bc27e7/src/lib/select/select.ts#L394-L402
It looks like the
ControlValueAcessorregistered "onChange" function should have been called too:crisbeto commentedon Mar 2, 2018
Thanks for looking into it @julianobrasil, that is definitely the issue.
fix(select): value set through property not being propagated to value…
fix(select): value set through property not being propagated to value…
mdklab commentedon Apr 25, 2018
What is the status of pull request merging?
casizzi commentedon May 14, 2018
I have an issue where I cannot set the value of the
<mat-select>when using in conjuction with a reactive form group, is this also related to this issue?fix(select): value set through property not being propagated to value…
mluis commentedon May 29, 2018
I'm having the same issue as @casizzi .
"@angular/material": "^5.2.4"casizzi commentedon May 29, 2018
@mluis if you set the value in the formGroup to use one of the mat-select-options then the view will update like this https://stackblitz.com/edit/angular-material2-issue-3eafh1?file=app/app.component.ts,
or you can simply use an ngModel on the select to kinda fake it with a new value like here https://stackoverflow.com/questions/50487942/set-select-input-value-via-angular-fromcontrol-value/50488087?noredirect=1#comment87989723_50488087
10 remaining items
fix(select): value set through property not being propagated to value…
fix(select): value set through property not being propagated to value…
Barna001 commentedon May 26, 2020
@rip222 's solution works:
Thomseeen commentedon Jul 21, 2020
Any updates on this?
I got options defined by
A reactive form set by a
formBuilder:And the group in HTML as:
But the form does never display the given inital value and no matter what is choose,
this.quoteForm.valuewill always hold the inital value.EDIT: Fixed. Just missed the braces around
[value]so the select form never had any actual value to set the form to.jclima27 commentedon Jul 24, 2020
I'm having the same issue as @Thomseeen . "@angular/material": "^8.2.3" . How will I to edit a form with mat-select?
i think it is a serious failure
Thomseeen commentedon Jul 25, 2020
@clima27 as I have stated above after edit, I have fixed the problem. It was a simple mistake on my side. Check the stackblitz example from above
fix(material/select): value set through property not being propagated…
fix(material/select): value set through property not being propagated…
fix(material/select): value set through property not being propagated…
fix(material/select): value set through property not being propagated…
angular-automatic-lock-bot commentedon Apr 23, 2022
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.