第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為 *ngIf 中聲明的模板調(diào)用@ViewChild?

如何為 *ngIf 中聲明的模板調(diào)用@ViewChild?

躍然一笑 2021-11-18 17:07:18
我在表內(nèi)動態(tài)調(diào)用一個組件。我正在使用 ViewContainerRef 在模板內(nèi)動態(tài)呈現(xiàn)組件。但是另一個元素內(nèi)的模板沒有被填充。也許是因為在聲明時@ViewChild,另一個 ng-template 不在 DOM 中。下面是組件代碼:@ViewChild('loadComponent', {static: false, read: ViewContainerRef}) ref;ngAfterViewChecked(): void {    const componentFactory =     this._componentFactoryResolver.resolveComponentFactory(DynamicComponent);    const ref = this.ref.createComponent(componentFactory);    ref.changeDetectorRef.detectChanges();  }這是模板代碼:<table>...    <tr *ngFor="let data of dataItems">              <td *ngIf="data.isDisplay">               <ng-template #loadComponent></ng-template> // this does'nt work      </td>...那么,td一旦td代碼被動態(tài)評估,我如何確保組件在內(nèi)部呈現(xiàn)?
查看完整描述

1 回答

?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

編輯:


通過非靜態(tài)獲取所有 templateRefs,您可以遍歷模板 refs 并為每個組件創(chuàng)建一個組件,之后您只需要將其附加到某個 DOM 元素(templateRef 的父元素)


@ViewChildren("dynamicTemplateId") private refs: QueryList<"dynamic">;


this.refs.forEach((r: any) => {

  // Create a new instance of the component

  const dynamicComponentRef = componentFactory.create(this.injector);


  // If you don't attach to appRef ng hook cycles won't work inside the component 

  // You can skip adding to application ref but you'll be needing to call .detectChanges() for every changes 

  this.appRef.attachView(dynamicComponentRef.hostView);


  // Append to parentElement since templateRef isn't a element itself

  r.elementRef.nativeElement.parentElement.appendChild(dynamicComponentRef.location.nativeElement);

});

這是一個工作示例:https : //stackblitz.com/edit/angular-gmnpku?file=src/app/app.component.ts


查看完整回答
反對 回復 2021-11-18
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號