簡體   English   中英

如何獲取元素的ElementRef?

[英]How to get the ElementRef of an element?

我正在使用DCL loadnexttolocation加載組件。 有沒有辦法可以在指定的元素中加載組件。 我的意思是我想在默認情況下更改它加載的位置。 這是我的插件: http ://plnkr.co/edit/2dKxNAOgqYqGn4n0u8PT?p=preview

add() {
  this._dcl.loadNextToLocation(DynamicCmp, this._elementref);
}

這是我提出事件時的代碼.. elementref始終考慮根組件。 有沒有辦法可以在根目錄中找到子元素的elementref?

我需要的是要在此元素中加載的組件:

<div #location id="location"></div>

添加模板變量並使用@ViewChild()查詢該元素

@Component({selector: 'some-selector',
  template: `
  <div #location id="location"></div>      `
export class MyComponent {
  // only set after `ngAfterViewInit`
  @ViewChild('location') location;
  constructor(private _dcl: DynamicComponentLoader, private _elementref: ElementRef) {}

  add() {
    this._dcl.loadNextToLocation(DynamicCmp, this.location);
  }
}

Plunker

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM