如何只用ng-container创建一个if-else Angular模板?

Tho*_*ous 19 if-statement angular-template angular

我想在我的角度模板中做一个if-else语句.我从那开始:

<ng-container *ngIf="contributeur.deb; else newDeb" >
    [... HERE IS A RESULT 1]
</ng-container>
<ng-template #newDeb>
    [... HERE IS A RESULT 2]
</ng-template>
Run Code Online (Sandbox Code Playgroud)

我试图只使用ng-container:

<ng-container *ngIf="contributeur.deb; else newDeb" >
    [... HERE IS A RESULT 1]
</ng-container>
<ng-container #newDeb>
    [... HERE IS A RESULT 2]
</ng-container >
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用.我有这个错误:

ERROR TypeError: templateRef.createEmbeddedView is not a function
    at ViewContainerRef_.createEmbeddedView (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:10200:52)
    at NgIf._updateView (eval at <anonymous> (vendor.bundle.js:96), <anonymous>:2013:45)
    at NgIf.set [as ngIfElse] (eval at <anonymous> (vendor.bundle.js:96), <anonymous>:1988:18)
    at updateProp (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:11172:37)
    at checkAndUpdateDirectiveInline (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:10873:19)
    at checkAndUpdateNodeInline (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:12290:17)
    at checkAndUpdateNode (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:12258:16)
    at debugCheckAndUpdateNode (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:12887:59)
    at debugCheckDirectivesFn (eval at <anonymous> (vendor.bundle.js:11), <anonymous>:12828:13)
    at Object.eval [as updateDirectives] (ActionsButtons.html:5)
Run Code Online (Sandbox Code Playgroud)

谁能解释一下这段代码出了什么问题?

Joe*_*lay 36

ngIf指令的代码期望传递TemplateRef对else分支的template()的引用,它将调用createEmbeddedView它来显示嵌套内容.因此,尝试对else内容使用任何其他类型的元素是没有意义的 - 它只是不起作用.但是,如果需要,你可以在ng-container 内部嵌套ng-template.

这可能看起来不直观,但要记住,结构指令(即你用a调用的那些*)总是表示为ng-template引擎盖,无论它们附加什么类型的元素 - 这两段代码是相同的:

<ng-container *ngIf="contributeur.deb; else newDeb" >
    ...
</ng-container>
<ng-template #newDeb>
    ...
</ng-template>
Run Code Online (Sandbox Code Playgroud)
<ng-template [ngIf]="contributeur.deb; else newDeb">
    <ng-container>
        ...
    </ng-container>
</ng-template>
<ng-template #newDeb>
    ...
</ng-template>
Run Code Online (Sandbox Code Playgroud)


rio*_*fly 13

我不喜欢if then else的标准 Angular 结构。然后我找到了ngSwitch的替代解决方案:

<ng-container [ngSwitch]="isFirstChoice(foo) ? 1 : (isSecondChoice(foo) ? 2 : -1)">
  <ng-container *ngSwitchCase="1">
    ...first choice...
  </ng-container>
  <ng-container *ngSwitchCase="2">
    ...second choice...
  </ng-container>
  <ng-container *ngSwitchDefault>
    ...another choice...
  </ng-container>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

为了回答你的要求,我会用这个:

<ng-container [ngSwitch]="contributeur.deb && 'isDeb'">
  <ng-container *ngSwitchCase="'isDeb'">
    ......
  </ng-container>
  <ng-container *ngSwitchDefault>
    ......
  </ng-container>
</ng-container>
Run Code Online (Sandbox Code Playgroud)


Ada*_*ise 6

值得一提的是,这个简单的替代方案可能比许多其他选项更具可读性。只需使用第二个 ng-container,然后用感叹号“not”条件即可创建 else 部分。

<ng-container *ngIf="contributeur.deb" >
    [... HERE IS A RESULT 1]
</ng-container>
<ng-container *ngIf="!contributeur.deb">
    [... HERE IS A RESULT 2]
</ng-container >
Run Code Online (Sandbox Code Playgroud)