TemplateRef 没有提供者!(NgIf ->TemplateRef) [英] No provider for TemplateRef! (NgIf ->TemplateRef)
问题描述
如果答案是接受的答案,我会尝试显示一个复选标记:
I'm trying to show a checkmark if an answer is the accepted answer:
template: `<div ngIf="answer.accepted">✔</div>`
但我收到此错误:
EXCEPTION: No provider for TemplateRef! (NgIf ->TemplateRef)
我做错了什么?
推荐答案
你错过了 NgIf 前面的 *
(就像我们都有,几十次一样):
You missed the *
in front of NgIf (like we all have, dozens of times):
<div *ngIf="answer.accepted">✔</div>
<小时>
如果没有 *
,Angular 会看到 ngIf
指令被应用到 div
元素,但是因为没有 *
或 标签,无法定位模板,因此报错.
Without the *
, Angular sees that the ngIf
directive is being applied to the div
element, but since there is no *
or <template>
tag, it is unable to locate a template, hence the error.
如果您在使用 Angular v5 时遇到此错误:
If you get this error with Angular v5:
错误:StaticInjectorError[TemplateRef]:
StaticInjectorError[TemplateRef]:
NullInjectorError: 没有 TemplateRef 的提供者!
Error: StaticInjectorError[TemplateRef]:
StaticInjectorError[TemplateRef]:
NullInjectorError: No provider for TemplateRef!
您的一个或多个组件模板中可能有 ...
.将标签更改/更新为
.
You may have <template>...</template>
in one or more of your component templates. Change/update the tag to <ng-template>...</ng-template>
.
这篇关于TemplateRef 没有提供者!(NgIf ->TemplateRef)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!