So this is the working code:
<mat-form-field *ngIf="asFormFieldWithLabel; else selector" style="width: 100%;">
<mat-label>{{asFormFieldWithLabel}}</mat-label>
<mat-select
...bindings and event crap...
>
<mat-option *ngFor="let folder of folderList" [value]="folder">
{{ folder.id.folderName }}
</mat-option>
</mat-select>
</mat-form-field>
<ng-template #selector>
<mat-select
...SAME bindings and event crap...
>
<mat-option *ngFor="let folder of folderList" [value]="folder">
{{ folder.id.folderName }}
</mat-option>
</mat-select>
</ng-template>
As I try to avoid duplicated code, I thought this should do it as well:
<mat-form-field *ngIf="asFormFieldWithLabel; else selector" style="width: 100%;">
<mat-label>{{asFormFieldWithLabel}}</mat-label>
<ng-container *ngTemplateOutlet="selector"></ng-container>
</mat-form-field>
<ng-template #selector>
<mat-select
...bindings and event crap...
>
<mat-option *ngFor="let folder of folderList" [value]="folder">
{{ folder.id.folderName }}
</mat-option>
</mat-select>
</ng-template>
Error is the infamous
Error: mat-form-field must contain a MatFormFieldControl
in the 2nd case.
Why though? Do I really need to repeat all the mat-select stuff?
It looks like this template is not needed, we can achieve the same with just an
ngIf
on thelabel
I hope this helpshtml
ts
stackblitz demo