Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
set mat-select value by formcontrol without ngModel

may I set selected Value by formcontrol i Have this snipped in my html document

  <!--segment panel-->
  <fa-expansion-panel class="expansion-panel-class" [title]="'Сегмент'">
    <mat-form-field
      floatLabel="always"
      class="segment-field fa-input-field full-width"
    >
      <mat-label>Обрані пункти</mat-label>
      <mat-select [formControlName]="'segments'" multiple>
        <mat-option
          class="mat-options"
          *ngFor="let segment of segmentList"
          [value]="segment"
          >{{ segment.segName }}</mat-option
        >
      </mat-select>
    </mat-form-field>
  </fa-expansion-panel>
  <!--end segment panel-->

in the ts file i have this form control

segments = new FormControl()

this is the segments list

segmentList = [
 {id: 1, segName: 'hello'},
 {id: 2, segName: 'by'},
 {id: 3, segName: 'welcome'},
]

here I'm changing the form control value

segments.setValue([{id: 2, segName: 'by'}, {id: 3, segName: 'welcome'}])

the code compiles successfully, but no items are selected in the select box

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It probably has to do with how Javascript handles objects. Let me explain.

Object in javascript are checked by reference. So let's say your segmentList

segmentList = [
 {id: 1, segName: 'hello'},
 {id: 2, segName: 'by'},
 {id: 3, segName: 'welcome'},
]

is equivalent to these references:

segmentList = [
  hash1abc,
  hash2edf,
  hash3ghi,
];

so when you select something, the formControl becomes equal to hashxxx reference, and it is shown by the value of hash1abc.segName.

If you follow me so far, you'll find the solution easily:

so when you do

segments.setValue([{id: 2, segName: 'by'}, {id: 3, segName: 'welcome'}])

you are doing:

segments.setValue([diffHashabc, diffHashcef])

your objects look the same, but they are indeed not the same ones that are on the list. So your mat-form does not recognize them. This happens because the array inside the setValue is a different set of Objects with different references.

I would try two things (unsure if these would solve the issue though), instead of writing them by hand, try getting them from your segmentList through filter or find.

Or try to use trackBy: segmentFn on your ngFor so it reads the id of the objects to know which one to render, instead of Object reference. Unsure if this would work but might. You would also need to add to the component segmentFn = (index, segment) => segment.id; for it to work

about 4 years ago · Juan Pablo Isaza Report

0

Try this:

<mat-select [formControlName]="'segments'" multiple
       [compareWith]="compareObjects">
        <mat-option
          class="mat-options"
          *ngFor="let segment of segmentList"
          [value]="segment">
           {{ segment.segName }}
       </mat-option>
 </mat-select>

in the ts file define the compareObjects like:

compareObjects(o1: any, o2: any): boolean {
  return o1.segName === o2.segName && o1.id === o2.id;
}
about 4 years ago · Juan Pablo Isaza Report

0

You've created different objects, whereas Angular Material will compare by reference. Set the form by referencing the original objects:

segments.setValue([this.segmentList[1], this.segmentList[2]])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!