Angular Material mat-select dynamic data binding in Angular

monir tuhin picture monir tuhin · Jul 2, 2018 · Viewed 31k times · Source

I am using Angular 6 and Angular Material. When i click on Edit button the Secondary, SSC and Male Value will be initialized on the select. But i can not able to do it. I only able to show Male value on the drop down after clicking Edit button. So i want to show all value on the drop down and pass object for the dynamic selection. Thanks.

My code link here: stackblitz link

Answer

Krishna Rathore picture Krishna Rathore · Jul 2, 2018

You can try with this solution

I have create a demo on Stackblitz

Component.ts

  editInfo(educationInfo) {
    this.education_level = educationInfo.aa;
    this.exam_title = educationInfo.bb;
    this.gender = educationInfo.cc;
    this.educationLevelChangeAction(this.education_level);
  }

  educationLevelChangeAction(education) {
    this.exam_title = "";
    let dropDownData = this.educationList.find((data: any) => data.educationLevelName === education);
    if (dropDownData) {
      this.degreeTitleList = dropDownData.degreeTitleList;
    } else {
      this.degreeTitleList = [];
    }

  }

Component.html

<mat-form-field>
  <mat-select placeholder="Select Level of Education" name="education_level" (selectionChange)="educationLevelChangeAction(education_level)" [(ngModel)]="education_level" >
    <mat-option *ngFor="let education of educationList" [value]="education.educationLevelName" >{{ education.educationLevelName }}</mat-option>
  </mat-select>
</mat-form-field>

<mat-form-field>
  <mat-select placeholder="Select Exam/Degree Title" name="exam_title" [(ngModel)]="exam_title">
    <mat-option *ngFor="let degreeTitle of degreeTitleList" [value]="degreeTitle">{{ degreeTitle }}</mat-option>
  </mat-select>
</mat-form-field>

<mat-form-field>
  <mat-select [(ngModel)]="gender">
    <mat-option *ngFor="let gender of genderList" [value]="gender">{{ gender }}</mat-option>
  </mat-select>
</mat-form-field>



<p>You selected: {{education_level}}  {{exam_title}} {{gender}}</p>