访问模板元素内的模板参考

编程入门 行业动态 更新时间:2024-10-06 14:33:16
本文介绍了访问模板元素内的模板参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用一个库,希望我将指令的主体指定为template元素的子代

I'm using a library that expects me to specify body of a directive as a child of template element

<template customDirective> <custom-element #lookup></custom-element> </template>

是否可以在组件内部访问custom-element#lookup.

Is there a way to access custom-element#lookup inside my component.

例如,

@Component({ selector: 'app-test', template: ` <template customDirective> <custom-element #lookup></custom-element> </template> ` }) export class TestComponent { @ViewChild('lookup') viewChildRef; @ContentChild('lookup') contentChildRef; constructor() { } ngAfterContentInit(): void { console.log(this.viewChildRef); // <-- undefined console.log(this.contentChildRef); // <-- undefined } }

在两种情况下我都得到undefined.

I'm getting undefined in both cases.

推荐答案

在不创建嵌入式视图之前,您无法获得对模板内部组件的引用.

You cannot get reference to component inside template until you don't create embedded view.

尝试使用以下设置器:

@ViewChild('lookup') set lookUp(ref: any) { console.log(ref); }

柱塞示例

Plunker Example

更多推荐

访问模板元素内的模板参考

本文发布于:2023-08-03 06:33:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!