0

I have following angular material select HTML:

<mat-select formControlName='personPersonDocumentTypes'>
<mat-option *ngFor="let dokumeti of MOQALAQEdokumentisTipebi" [value]="dokumeti">
{{dokumeti}}
</mat-option>
</mat-select>

MOQALAQEdokumentisTipebi is an array that is getting updated from a network API request. I just want to select by default the first element of 'MOQALAQEdokumentisTipebi' array.

I have tried doing something like this:

<mat-select [(ngModel)]="MOQALAQEdokumentisTipebi[0]" formControlName='personPersonDocumentTypes'>
<mat-option *ngFor="let dokumeti of MOQALAQEdokumentisTipebi" [value]="dokumeti">
{{dokumeti}}
</mat-option>
</mat-select>

But in this case, I would get the whole array instead of the first element.

I have tried adding default variable in my TS file:

public defValue = MOQALAQEdokumentisTipebi[0];

But in this case, I was getting blank selection, as if there was nothing assigned to it. I also console logged my defValue variable and it showed data, but it didn't work in default selection.

My question is how to set default selection first element of the array in the code like this(with ngFor):

<mat-select formControlName='personPersonDocumentTypes'>
<mat-option *ngFor="let dokumeti of MOQALAQEdokumentisTipebi" [value]="dokumeti">
{{dokumeti}}
</mat-option>
</mat-select>

// adding array of doc types


this.moqalaqeSearchData.personPersonDocumentTypes.map((array) => {
this.MOQALAQEdokumentisTipebi.push(array.personDocumentType.nameGeorgian);
});
8
  • Does it help you ? stackoverflow.com/a/50651042/6444705 Commented Mar 19, 2021 at 10:21
  • Value also gives no selection, I have tried it Commented Mar 19, 2021 at 10:23
  • Probably it is something with using array as an option and for a cycle. How to make work default value, in this case, is the question Commented Mar 19, 2021 at 10:24
  • Don't use ngModel and formControlName together. Use form only. When you get response do this.form.get('personPersonDocumentTypes').patchValue(MOQALAQEdokumentisTipebi[0]). Commented Mar 19, 2021 at 10:28
  • But how to set default select value? How to assign it in here - <mat-select formControlName='personPersonDocumentTypes'> Commented Mar 19, 2021 at 10:33

1 Answer 1

1

As @Bojan said, you need to set the form value when you receive datas :

form: FormGroup;

constructor() {
  this.form = new FormGroup({
    personPersonDocumentTypes: [''],
    // ...
  });

  this.moqalaqeSearchData.personPersonDocumentTypes.map((array) => {
    this.MOQALAQEdokumentisTipebi.push(array.personDocumentType.nameGeorgian);
  });

  this.form.get('personPersonDocumentTypes').patchValue(this.MOQALAQEdokumentisTipebi[0])
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.