gpt4 book ai didi

Angular 6 : given ElementRef, 我可以得到 ViewContainerRef

转载 作者:行者123 更新时间:2023-12-03 23:15:33 26 4
gpt4 key购买 nike

模板

<div my-directive></div>

指示
constructor(vc: ViewContainerRef, el: ElementRef, svc: MyService) {}

useService() {
this.svc.doWork(vc, el);
}

所以该服务需要一个 ViewContainerRef和一个 ElementRef .我必须通过这两个吗?有没有办法从元素 ref 派生 View 容器?如果是这样,我只需要通过 ElementRef到服务,它会从中获取 View 容器。

或者,可以 ElementRef源自 ViewContainerRef ?似乎不可能,因为我假设单个 View 容器可以有多个元素。

最佳答案

您可以使用 View 中的元素引用。它们都引用相同的 DOM 元素。

constructor(vc: ViewContainerRef, el: ElementRef, svc: MyService) {
console.log(vc.element.nativeElement === el.nativeElement);
// prints true
this.svc.doWork(vc, vc.element);
}

关于Angular 6 : given ElementRef, 我可以得到 ViewContainerRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51729076/

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