Mar*_*cok 217 angular2-template angular
如果答案是可接受的答案,我试图显示一个复选标记:
template: `<div ngIf="answer.accepted">✔</div>`
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
EXCEPTION: No provider for TemplateRef! (NgIf ->TemplateRef)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Mar*_*cok 519
你错过了*
NgIf的面前(就像我们都有,几十次):
<div *ngIf="answer.accepted">✔</div>
Run Code Online (Sandbox Code Playgroud)
如果没有*
,Angular会看到该ngIf
指令正在应用于该div
元素,但由于没有*
或<template>
标记,它无法找到模板,因此错误.
如果Angular v5出现此错误:
错误:StaticInjectorError [TemplateRef]:
StaticInjectorError [TemplateRef]:
NullInjectorError:没有TemplateRef的提供程序!
您可能拥有<template>...</template>
一个或多个组件模板.将标签更改/更新为<ng-template>...</ng-template>
.
小智 5
因此,我可以帮助您了解您犯了什么错误(以及在哪里),但在此之前,我们需要阅读文档:
通过在元素上放置指令(或以 * 为前缀的指令)来访问 TemplateRef 实例。嵌入视图的 TemplateRef 使用 TemplateRef 标记注入到指令的构造函数中。
您还可以使用 Query 来查找与组件或指令关联的 TemplateRef。
所以:
[ngIf]="something"
您必须使用<ng-template>
来访问模板 ref(例如:使用<template>
或时<ng-template>
);*ngIf
你可以在你想要的地方使用它,因为 * 保证访问模板引用(例如:当使用<span>
或时<div>
);如果您在代码中使用类似的东西,<span [ngIf]="message"> {{message}}</span>
您可能会遇到一些错误。
归档时间: |
|
查看次数: |
85707 次 |
最近记录: |