gpt4 book ai didi

angular - 使用 TemplateRef 的简单指令产生 "No provider for TemplateRef"

转载 作者:行者123 更新时间:2023-12-05 04:10:05 24 4
gpt4 key购买 nike

我正在尝试创建一个根据条件创建 View 的指令。就像内置的 ngIf 指令一样。

这是我添加到项目中的所有内容:

export class QueryNameContext {
public name: string;
}

@Directive({
selector: '[action]'
})
export class QueryNameDirective {
@Input('action')
public name: string;

public constructor(private viewContainerRef: ViewContainerRef,
private templateRef: TemplateRef<QueryNameContext>) {
}
}

当我尝试在模板中使用指令时:

<div *action="thing"></div>

我得到这个依赖错误:

core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: No provider for TemplateRef!

我的模块正在导入 CommonModule 并且我能够在 @ViewChild 中使用 TemplateRef 其他指令的绑定(bind)。

我看不出我做错了什么。

最佳答案

也许有人会遇到这个问题,所以我会把它留在这里。

我有 两个 指令错误地使用了相同的选择器。重命名此修复了 Uncaught (in promise): Error: No provider for TemplateRef! 错误。

关于angular - 使用 TemplateRef 的简单指令产生 "No provider for TemplateRef",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44958827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com