“ Angular 6 ng build --prod错误”错误错误:ngIfElse必须是TemplateRef,但是收到了'true'。

时间:2018-10-15 15:55:25

标签: javascript typescript angular6

错误错误: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)

3 个答案:

答案 0 :(得分:9)

对于带有其他块的ngIf,您需要在<ng-template>文件中将.html块用作其他块。

这是示例代码:

<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>

答案 1 :(得分:0)

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

  

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

或尝试使用以下内容更改此代码行。

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

如果仍然遇到问题,只需编辑代码并将其保存在上述url上。我会看看。

答案 2 :(得分:0)

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

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