没有TemplateRef的提供者! (NgIf-> TemplateRef) [英] No provider for TemplateRef! (NgIf ->TemplateRef)

查看:122
本文介绍了没有TemplateRef的提供者! (NgIf-> TemplateRef)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果答案是可接受的答案,我会尝试打勾:

I'm trying to show a checkmark if an answer is the accepted answer:

template: `<div ngIf="answer.accepted">&#10004;</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">&#10004;</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-&gt; TemplateRef)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆