angular - 如何获取对角度结构指令容器内元素的引用
问题描述
我有以下模板,其中包含一个名为的测试结构指令*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
解决方案
推荐阅读
- django - Django:如何获取具有文件名的对象
- rest - 哪个轻:REST 或 GRPC
- typescript - 打字稿:对象可能为空
- javascript - 如何比较两个对象数组并删除javascript中的重复项
- sequelize.js - 使用 Sequelize 关联模型的方法是否正确?
- mediawiki - 无论如何,mediawiki 页面有多个显示标题?
- spring - spring-boot-starter-quartz jdbc vs不同环境下的内存jobstore
- mediawiki - 带有斜杠的页面标题上的权限问题真的很奇怪
- c# - 是否可以配置 .net runsettings 文件以包含复杂对象?
- reactjs - 通过反应 js lodash 循环并更改字段值