我试图在带有 template-outlet 的 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>
Run Code Online (Sandbox Code Playgroud)
我不想html在每个*ngSwitchCase块中编写代码,而是想添加对 a 的引用ng-template
(来自 angular docs):
<div *ngIf="show; then thenBlock; else elseBlock">this is ignored</div>
<ng-template #primaryBlock>Primary text to show</ng-template>
Run Code Online (Sandbox Code Playgroud)
想要这样做:
<div [ngSwitch]="switchVar">
<div *ngSwitchCase="1; then myTemplate"></div>
<div *ngSwitchDefault>output2</div>
</div>
<ng-template #myTemplate>HTML TEXT</ng-template>
Run Code Online (Sandbox Code Playgroud)
而不是这个:
<div [ngSwitch]="switchVar">
<div *ngSwitchCase="1">HTML TEXT</div>
<div *ngSwitchDefault>output2</div>
</div>
Run Code Online (Sandbox Code Playgroud)