我正在尝试在带有模板出口的mat-select中传递#tmp,但无法显示选择选项。下面是我的代码和stackblitz的链接
<mat-form-field>
<mat-select
[ngModel]="selectedFoods"
(ngModelChane)="selectedFoods" placeholder="Favorite food" multiple>
<ng-container *ngFor="let food of allfoods"
[ngTemplateOutlet]="tmp"
[ngTemplateOutletContext]="{ $implicit: food}">
</ng-container>
</mat-select>
</mat-form-field>
<ng-template #tmp let-food>
<mat-option [value]="food.value">
{{food.viewValue}}
</mat-option>
</ng-template>
答案 0 :(得分:2)
这似乎可行。我认为重要的部分仍然是<mat-options>
内有<mat-select>
,而不是模板的一部分。
<mat-form-field>
<mat-select>
<mat-option *ngFor="let food of allfoods">
<ng-container [ngTemplateOutlet]="tmp" [ngTemplateOutletContext]="{food: food}">
</ng-container>
</mat-option>
</mat-select>
</mat-form-field>
<ng-template #tmp let-food="food">
{{food.viewValue}} <b> From Tmp</b>
</ng-template>