Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda