Search code examples
angularformsselectsetvalueangular-reactive-forms

Angular 5 set select object value manually


I have one select, and I want to set an object value manually. I tried to use setValue or patchValue with Object, but it did not work.

<mat-select placeholder="Selecione a resposta" (change)="onSelectFraudResponse($event.value)" formControlName="response">
  <mat-option *ngFor="let res of fraudResponses" [value]="res">
    {{res.name}}
  </mat-option></mat-select>

I know is incorrect using template form and reactive form together.


Solution

  • For binding objects you need to use [ngValue] since [value] is used for primitive types

    EDIT:

    Since i didn't see a concrete example how you can two-way bind an object inside mat-select, i would advise you use a property which will identify your object and instead of changing the whole object, you can just change that value, the right attribute for two-way databinding is [(value)]