如何解决如何获取对角度结构指令容器内元素的引用
我有以下模板,其中包含一个名为 *appUnless
的测试结构指令:
<h1 *appUnless>
<app-hello></app-hello>
<input type="text" value="hello" />
</h1>
在我的结构指令中,到目前为止我有这个:
import {
Directive,Input,TemplateRef,ViewContainerRef,} from "@angular/core";
@Directive({
selector: "[appUnless]",})
export class UnlessDirective {
constructor(
private templateRef: TemplateRef<any>,private viewContainer: ViewContainerRef
) {}
@Input()
set appUnless(message: string) {
const embeddedViewRef = this.viewContainer.createEmbeddedView(
this.templateRef
);
}
}
如何从指令中获取对 input
元素的引用?我希望能够引用 input
与在其上放置模板引用声明没有什么不同,即,如果我要执行 <input #myInput>
。显然,由于我无法控制容器内放置的内容,因此无法将 #myInput
放入模板中。
此外,如何从该指令中获得对具有正确类型的 AppHelloComponent
(支持 app-hello
)的引用?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。