垫选择内的模板出口不起作用

ano*_*ous 4 angular angular-content-projection

我试图在带有 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)

https://stackblitz.com/edit/angular-mat-select-with-ngmodel-mujorn?embed=1&file=app/select-overview-example.ts

Ste*_*per 5

这似乎有效。我认为重要的部分仍然是内部<mat-options><mat-select>不是模板的一部分。

https://stackblitz.com/edit/mat-select-with-ngtemplateoutlet-example?devtoolsheight=33&file=app/select-overview-example.html

<mat-form-field>
    <mat-select>
       <mat-option *ngFor="let food of allfoods"  [value]="food.value">
          <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>


Run Code Online (Sandbox Code Playgroud)

在选择中显示模板值的下拉列表