相关疑难解决方法(0)

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

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

angular angular-content-projection

4
推荐指数
1
解决办法
2055
查看次数

在 ngSwitch 中使用 ng-template 参考

我不想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)

angular

3
推荐指数
1
解决办法
3266
查看次数