【问题标题】:Angular 6 ng build --prod error "ERROR Error: ngIfElse must be a TemplateRef, but received 'true'."Angular 6 ng build --prod 错误“错误错误:ngIfElse 必须是 TemplateRef,但收到‘真’。”
【发布时间】:2018-10-15 15:55:25
【问题描述】:

ERROR 错误:ngIfElse 必须是 TemplateRef,但接收到 'true'

HTML 文件

<select class="form-control">
  <option selected value="0">Select Manufacturer</option>
  <option *ngFor="let brand of allMakes">
     {{ brand.brand }}
  </option>
</select>

ts 文件

this.allMakes = [{
  id: '1',
  brand: 'Mazda'
},
{
  id: '2',
  brand: 'Audi',
}];

这一切都是在我从 Angular 5 升级到 Angular 6 之后开始的

控制台日志错误

ERROR Error: ngIfElse must be a TemplateRef, but received 'true'.
  at Vt (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at t.set [as ngIfElse] (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at Sa (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at main.6e360f04b7d39ee1dc33.bundle.js:1
  at main.6e360f04b7d39ee1dc33.bundle.js:1
  at es (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at Os (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at Object.updateDirectives 
  (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at Object.updateDirectives (main.6e360f04b7d39ee1dc33.bundle.js:1)
  at ts (main.6e360f04b7d39ee1dc33.bundle.js:1)

【问题讨论】:

  • 你确定这是导致错误的行吗?
  • 里面没有ngIf,请贴出所有代码

标签: javascript angular typescript


【解决方案1】:

对于带有 else 块的 ngIf,您需要在 .html 文件中使用 &lt;ng-template&gt; 块作为 else。

这里是示例代码:

<div *ngIf="condition; else elseBlock">Content to render when condition is true.</div> <ng-template #elseBlock>Content to render when condition is false.</ng-template>

【讨论】:

  • 我在 else 块的代码中使用了 &lt;ng-container&gt;。将其更改为 &lt;ng-template&gt; 解决了我的问题。谢谢!
【解决方案2】:

问题已解决,原因是附加到“ng-template”的冗余属性(Bool)

noData: boolean = true;
<ng-template #noData>...Blah blah...</ng-template>

【讨论】:

    【解决方案3】:

    在您的代码中使用 --prod 选项构建时未发现错误。 我已经为您创建了一个编辑器来解决您的问题(如果有的话)。我已经复制了你的代码并且它工作正常。

    https://stackblitz.com/edit/angular-p4ctul

    或尝试将这行代码更改为以下代码。

    platformBrowserDynamic().bootstrapModule(AppModule, { enableLegacyTemplate: true })
    

    如果您仍然遇到问题,只需在上面的 url 上编辑并保存您的代码。我去看看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2022-09-25
      相关资源
      最近更新 更多