首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作一个只有ng-container的if-else角模板?

如何制作一个只有ng-container的if-else角模板?
EN

Stack Overflow用户
提问于 2017-05-31 22:44:51
回答 3查看 50.7K关注 0票数 38

我想在我的angular模板中做一个if-else语句。我是从这个开始的:

代码语言:javascript
复制
<ng-container *ngIf="contributeur.deb; else newDeb" >
    [... HERE IS A RESULT 1]
</ng-container>
<ng-template #newDeb>
    [... HERE IS A RESULT 2]
</ng-template>

我试着只使用ng-container:

代码语言:javascript
复制
<ng-container *ngIf="contributeur.deb; else newDeb" >
    [... HERE IS A RESULT 1]
</ng-container>
<ng-container #newDeb>
    [... HERE IS A RESULT 2]
</ng-container >

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

代码语言:javascript
复制
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)

有人能给我解释一下这段代码出了什么问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-31 23:02:51

The code for the ngIf directive需要传递一个对else分支的模板(TemplateRef)的引用,它将在该模板上调用createEmbeddedView以显示嵌套内容。因此,尝试对else内容使用任何其他类型的元素是没有意义的-它不会起作用。但是,如果需要的话,您可以在ng-template中嵌套一个ng-container

这可能看起来不直观,但请记住,structural directives (即使用*调用的元素)总是在幕后表示为ng-template,无论它们附加到哪种类型的元素上-这两段代码是相同的:

代码语言:javascript
复制
<ng-container *ngIf="contributeur.deb; else newDeb" >
    ...
</ng-container>
<ng-template #newDeb>
    ...
</ng-template>

代码语言:javascript
复制
<ng-template [ngIf]="contributeur.deb; else newDeb">
    <ng-container>
        ...
    </ng-container>
</ng-template>
<ng-template #newDeb>
    ...
</ng-template>
票数 77
EN

Stack Overflow用户

发布于 2019-11-29 22:55:24

我不喜欢if then else的标准角度结构。然后,我找到了使用ngSwitch的替代解决方案

代码语言:javascript
复制
<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>

为了回答你的请求,我会这样回答:

代码语言:javascript
复制
<ng-container [ngSwitch]="contributeur.deb && 'isDeb'">
  <ng-container *ngSwitchCase="'isDeb'">
    ......
  </ng-container>
  <ng-container *ngSwitchDefault>
    ......
  </ng-container>
</ng-container>
票数 14
EN

Stack Overflow用户

发布于 2021-10-22 23:44:08

值得一提的是,这个简单的替代方案可能比许多其他选项更具可读性。只需使用第二个ng容器,并用感叹号'not'-ing条件来构成else部分。

代码语言:javascript
复制
<ng-container *ngIf="contributeur.deb" >
    [... HERE IS A RESULT 1]
</ng-container>
<ng-container *ngIf="!contributeur.deb">
    [... HERE IS A RESULT 2]
</ng-container >
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44287846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档