Angular 5 + Material Design: <mat-select> how to set the default value?

 <mat-form-field>
      <mat-select [formControl]="form.controls['defaultCategory']" [(ngModel)]="resource.categoryName" >
        <mat-option *ngFor="let category of categories | async" [value]="category._id" >
          {{ category.name }}
        </mat-option>
      </mat-select>
 </mat-form-field>

I've tried a lot. The docs, some error messages on github still don't solve my problem.

I only need to preset the default value ...

The default value is stored in a variable named: resource.categoryName. The parameters are taken from the mongodb collection, and they are stored in categories [].

This image shows a closed mat-selection (the default value is not specified, but it should ...)

Mat selection open

Here is my .ts. The important line is " this.resource.categoryName = this.subdoc['name'];". There I set "resource.categoryName. If I try to use console.log (this.resource.categoryName), then I get the category name ...

.ts , . . ( , ) - , .

+4
1

, , . categoryName , . *.component.ts.

+1

Source: https://habr.com/ru/post/1690125/


All Articles