简体   繁体   English

没有 TemplateRef 的提供者! (NgIf -> 模板参考)

[英]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">&#10004;</div>`

But I get this error:但我得到这个错误:

EXCEPTION: No provider for TemplateRef! (NgIf ->TemplateRef)

What am I doing wrong?我究竟做错了什么?

You missed the * in front of NgIf (like we all have, dozens of times):你错过了 NgIf 前面的* (就像我们所有人一样,有几十次):

<div *ngIf="answer.accepted">&#10004;</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 会看到ngIf指令被应用于div元素,但由于没有*<template>标签,它无法定位模板,因此出现错误。


If you get this error with Angular v5:如果您在使用 Angular v5 时遇到此错误:

Error: StaticInjectorError[TemplateRef]:错误:StaticInjectorError[TemplateRef]:
StaticInjectorError[TemplateRef]:静态注入器错误[TemplateRef]:
NullInjectorError: No provider for TemplateRef! NullInjectorError: 没有 TemplateRef 的提供者!

You may have <template>...</template> in one or more of your component templates.您可能在一个或多个组件模板中有<template>...</template> Change/update the tag to <ng-template>...</ng-template> .将标签更改/更新为<ng-template>...</ng-template>

So, I can help to understand what (and where) you made the mistake, but before it, we need to read the documentation:因此,我可以帮助您了解您犯了什么错误(以及在哪里),但在此之前,我们需要阅读文档:

Access a TemplateRef instance by placing a directive on an element (or directive prefixed with *).通过在元素上放置指令(或以 * 为前缀的指令)来访问 TemplateRef 实例。 The TemplateRef for the embedded view is injected into the constructor of the directive, using the TemplateRef token.嵌入视图的 TemplateRef 使用 TemplateRef 标记注入到指令的构造函数中。

You can also use a Query to find a TemplateRef associated with a component or a directive.您还可以使用 Query 来查找与组件或指令关联的 TemplateRef。

So:所以:

  • When using [ngIf]="something" you must use <ng-template> to access the template ref (eg: when using <template> or <ng-template> );使用[ngIf]="something"您必须使用<ng-template>来访问模板引用(例如:使用<template><ng-template> );
  • Using *ngIf you can use it where you want 'cause the * garante the access to template ref(eg: when using <span> or <div> );使用*ngIf如果你可以在你想要的地方使用它,因为 * 保证访问模板引用(例如:当使用<span><div> );

If you're using in your code something like <span [ngIf]="message"> {{message}}</span> you probably gonna get some error.如果你在你的代码中使用了类似<span [ngIf]="message"> {{message}}</span>你可能会得到一些错误。

it's a common misconception to miss a few syntaxes in writing the code, one such popular mistake everyone tries to make is not using * before ngIf, which when used makes angular compiler to read ngIf as template reference variable and checks for local reference for the variable which wont be found in the file.在编写代码时遗漏一些语法是一种常见的误解,每个人都试图犯的一个常见错误是在 ngIf 之前不使用 *,当使用它时,angular 编译器将 ngIf 读取为模板引用变量并检查变量的本地引用在文件中找不到。 so please check for these mistakes and learn form them if possible所以请检查这些错误并尽可能从它们中学习

<div *ngIf="answer.accepted">&#10004;</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM