【发布时间】:2016-06-26 05:13:05
【问题描述】:
如果答案是可接受的答案,我会尝试显示复选标记:
template: `<div ngIf="answer.accepted">✔</div>`
但我收到此错误:
EXCEPTION: No provider for TemplateRef! (NgIf ->TemplateRef)
我做错了什么?
【问题讨论】:
如果答案是可接受的答案,我会尝试显示复选标记:
template: `<div ngIf="answer.accepted">✔</div>`
但我收到此错误:
EXCEPTION: No provider for TemplateRef! (NgIf ->TemplateRef)
我做错了什么?
【问题讨论】:
你错过了 NgIf 前面的*(就像我们一样,几十次):
<div *ngIf="answer.accepted">✔</div>
没有*,Angular 会看到ngIf 指令被应用于div 元素,但是由于没有* 或<template> 标签,它无法定位模板,因此错误。
如果您在使用 Angular v5 时遇到此错误:
错误:StaticInjectorError[TemplateRef]:
StaticInjectorError[TemplateRef]:
NullInjectorError: 没有 TemplateRef 的提供者!
您的一个或多个组件模板中可能包含<template>...</template>。将标签更改/更新为<ng-template>...</ng-template>。
【讨论】:
所以,我可以帮助您了解您犯了什么错误(以及在哪里犯了错误),但在此之前,我们需要阅读文档:
通过在 元素(或以 * 为前缀的指令)。模板参考 对于嵌入视图被注入到构造函数中 指令,使用 TemplateRef 令牌。
您还可以使用查询来查找与 组件或指令。
所以:
[ngIf]="something"时必须使用<ng-template>访问模板引用(例如:使用<template>或<ng-template>时);*ngIf,您可以在需要的地方使用它,因为*保证访问模板参考(例如:使用<span> 或<div> 时);如果您在代码中使用 <span [ngIf]="message"> {{message}}</span> 之类的东西,您可能会遇到一些错误。
【讨论】: