没有 TemplateRef 的提供者! (NgIf ->TemplateRef)
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 中遇到此错误:
Error: StaticInjectorError[TemplateRef]:
StaticInjectorError[TemplateRef]:
NullInjectorError: No provider for TemplateRef!
您的一个或多个组件模板中可能有 <template>...</template>
。 Change/update 标签到 <ng-template>...</ng-template>
.
所以,我可以帮助您了解您犯了什么(以及在哪里)错误,但在此之前,我们需要阅读文档:
Access a TemplateRef instance by placing a directive on an
element (or directive prefixed with *). The TemplateRef
for the embedded view is injected into the constructor of the
directive, using the TemplateRef token.
You can also use a Query to find a TemplateRef associated with a
component or a directive.
所以:
- 当使用
[ngIf]="something"
时,您必须使用 <ng-template>
来访问模板引用(例如:当使用 <template>
或 <ng-template>
时);
- 使用
*ngIf
你可以在你想要的地方使用它,因为 * 保证访问模板引用(例如:当使用 <span>
或 <div>
时);
如果您在代码中使用类似 <span [ngIf]="message"> {{message}}</span>
的内容,您可能会遇到一些错误。
如果答案是已接受的答案,我会尝试显示复选标记:
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 中遇到此错误:
Error: StaticInjectorError[TemplateRef]:
StaticInjectorError[TemplateRef]:
NullInjectorError: No provider for TemplateRef!
您的一个或多个组件模板中可能有 <template>...</template>
。 Change/update 标签到 <ng-template>...</ng-template>
.
所以,我可以帮助您了解您犯了什么(以及在哪里)错误,但在此之前,我们需要阅读文档:
Access a TemplateRef instance by placing a directive on an element (or directive prefixed with *). The TemplateRef for the embedded view is injected into the constructor of the directive, using the TemplateRef token.
You can also use a Query to find a TemplateRef associated with a component or a directive.
所以:
- 当使用
[ngIf]="something"
时,您必须使用<ng-template>
来访问模板引用(例如:当使用<template>
或<ng-template>
时); - 使用
*ngIf
你可以在你想要的地方使用它,因为 * 保证访问模板引用(例如:当使用<span>
或<div>
时);
如果您在代码中使用类似 <span [ngIf]="message"> {{message}}</span>
的内容,您可能会遇到一些错误。