[英]Can I convert a custom typescript component to a HTMLElement in Angular?
我相信 Angular Component 類並不代表 DOM 中的實際元素,它只不過是一個藍圖。 為了創建實際元素,您首先需要將其創建為 Angular 組件樹的一部分,然后通過ElementRef
對 dom 中實際元素的引用。
當然,這只有在一切都在 Angular 上下文中運行時才有可能。 在沒有 Angular 的情況下使用 Angular 組件的唯一方法是將其轉換為自定義元素,這是一個關於如何做到這一點的非常簡潔的教程https://medium.com/@lavanya.kakimallaiah/creating-a-custom-element-內角aa2a794fd041
如果注入構造函數 ElementRef,則可以在 elementRef.nativeElement 中“到達”
htmlRef:any
constructor(private elementRef:ElementRef)
{
this.htmlRef=elementRef.nativeElement
}
但真正重要的問題是:在 Angular 中需要 htmlRef 的原因是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.