没有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
元素,但是由于没有*
或<template>
标记,因此无法找到模板,因此错误.
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!
您的一个或多个组件模板中可能包含<template>...</template>
.将标签更改/更新为<ng-template>...</ng-template>
.
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屋!