windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Angular: Get value from mat select based id

Asked By: Anonymous

I am creating a dynamic form with multiple dropdowns (mat-select). The value for the select is the key. After the user has selected all dropdowns I want the selected values to be shown. However, as the select only holds the key and not the value, I am confused how I show achieve my goal.

Option 1 – Use id as value binding

    <mat-form-field appearance="fill" *ngSwitchCase="'dropdown'" class="w-75">
      <mat-label>{{question.label}}</mat-label>
      <mat-select [formControlName]="question.key">
        <mat-option *ngFor="let opt of question.options" [value]="opt.key">
          {{opt.value}}
        </mat-option>
      </mat-select>
      <mat-error *ngIf="!isValid">
        {{question.label}} is required
      </mat-error>
    </mat-form-field>

Option 2 – Use object as value binding**

I tried earlier to bind the value of the option to be the object itself (opt), that fixed my issue. But instead I was not able to set a default value. In this case I tested with both [value] and [ngValue] without success, example below:

    <mat-form-field appearance="fill" *ngSwitchCase="'dropdown'" class="w-75">
      <mat-label>{{question.label}}</mat-label>
      <mat-select [formControlName]="question.key">
        <mat-option *ngFor="let opt of question.options" [value]="opt">
          {{opt.value}}
        </mat-option>
      </mat-select>
      <mat-error *ngIf="!isValid">
        {{question.label}} is required
      </mat-error>
    </mat-form-field>

Generates the dropdown just fine. In the case that I want to set the default value, the value is not being selected but it outputs that it is selected as it should..

    let testOption = {key: 17, value: "Country 17"};
    this.form.controls.country.setValue(testOption); //Updates not visible in mat-select
    console.log(this.form.controls.country.value); //Output shows {key: 17, value: "Country 17"}

Any tips on how to solve any of the two options? I really would like to avoid to write a generic function that the html calls for to get the value. (Like in ts doing Array.filter….)

Thanks!


Solution

Answered By: Anonymous

First, create a compare function for your option objects:

  compareOptions(option1: Option, option2: Option) {
    return option1.key === option2.key;
  }

Then supply this function to mat-select:

<form [formGroup]="form">
  <mat-form-field appearance="fill" class="w-75">
    <mat-label>Question</mat-label>
    <mat-select formControlName="question" [compareWith]="compareOptions">
      <mat-option *ngFor="let opt of options" [value]="opt">
        {{opt.value}}
      </mat-option>
    </mat-select>
  </mat-form-field>
</form>

Then, set the form value and the new object will be compared by your id ‘key’ instead:

this.form.get('question')?.setValue({key: 3, value: 'Country 3'});

https://stackblitz.com/edit/angular-wqvgfk?file=src%2Fapp%2Fselect-custom-trigger-example.ts


Alternatively, you could find the option in the array using the ID and set the form control value to that. Since it is the same object reference, you will not need a compare function:

 this.form.get('question')?.setValue(this.options.find(opt => opt.key === 3));

https://stackblitz.com/edit/angular-wqvgfk-ymkixm?file=src%2Fapp%2Fselect-custom-trigger-example.ts

techinplanet staff

instagram volgers kopen volgers kopen buy windows 10 pro buy windows 11 pro


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
Alanya escort Manavgat escort Fethiye escort Kemer escort Didim escort Çanakkale escort Aydın escort Muğla escort Tekirdağ escort Manisa escort Balıkesir escort Trabzon escort Elazığ escort Ordu escort Kütahya escort Isparta escort Rize escort Kahramanmaraş escort Yalova escort Giresun escort Yozgat escort Tokat escort Şanlıurfa escort Sivas escort Batman escort Erzurum escort Sinop escort Kırşehir escort Karaman escort Kırıkkale escort Bolu escort Amasya escort Niğde escort Uşak escort Edirne escort Çorum escort Osmaniye escort Zonguldak escort Van escort Erzincan escort Söke escort Bodrum escort Çerkezköy escort Akhisar escort Bandırma escort Ayvacık escort Akçaabat escort Karakoçan escort Altınordu escort Tavşanlı escort Eğirdir escort Ardeşen escort Afşin escort Altınova escort Bulancak escort Sorgun escort Erbaa escort Viranşehir escort Zara escort Kozluk escort Aziziye escort Ayancık escort Kaman escort Ermenek escort Keskin escort Gerede escort Göynücek escort Bor escort Banaz escort Havsa escort Osmancık escort Bahçe escort Alaplı escort Başkale escort Kemah escort Nazilli escort Fethiye escort Çorlu escort Alaşehir escort Altıeylül escort Biga escort Araklı escort Kovancılar escort Fatsa escort Simav escort Yalvaç escort Çayeli escort Dulkadiroğlu escort Çiftlikköy escort Espiye escort Sarıkaya escort Niksar escort Suruç escort Yıldızeli escort Sason escort Horasan escort Boyabat escort Mucur escort Sarıveliler escort Yahşihan escort Göynük escort Gümüşhacıköy escort Çamardı escort Eşme escort İpsala escort Sungurlu escort Hasanbeyli escort Çaycuma escort İpekyolu escort Refahiye escort Kuşadası escort Marmaris escort Süleymanpaşa escort Turgutlu escort Susurluk escort Gelibolu escort Of escort Ünye escort Domaniç escort Fındıklı escort Elbistan escort Çınarcık escort Tirebolu escort Akdağmadeni escort Turhal escort Eyyübiye escort Suşehri escort Yakutiye escort Gerze escort Mengen escort Merzifon escort Ulukışla escort Sivaslı escort Keşan escort Kadirli escort Ereğli escort Özalp escort Tercan escort Efeler escort Didim escort Çine escort Dalaman escort Menteşe escort Milas escort Ortaca escort Seydikemer escort Ergene escort Kapaklı escort Malkara escort Salihli escort Şehzadeler escort Soma escort Yunusemre escort Ayvalık escort Bigadiç escort Burhaniye escort Gönen escort Karesi escort Çan escort Yenice escort Ortahisar escort Yomra escort Perşembe escort Pazar escort Onikişubat escort Pazarcık escort Türkoğlu escort Eynesil escort Görele escort Piraziz escort Yağlıdere escort Çayıralan escort Boğazlıyan escort Zile escort Siverek escort Karaköprü escort Haliliye escort Akçakale escort Şarkışla escort Gemerek escort Oltu escort Palandöken escort Mudurnu escort Suluova escort Taşova escort Toprakkale escort Kilimli escort Tuşba escort Üzümlü escort