2

我試圖加載不同的「父」組件,並通過在每個父組件中定位ng-content將路由內容注入到這些不同的父組件中。基本上,每個父組件處理基於設備寬度(小/移動,中/平板和大/桌面)的導航和其他樣板材料。Angular 4 ngComponentOutlet加載動態組件,但路由內容未呈現

在我的應用程序中爲每條路徑生成的內容需要被轉移(注入)到父組件中。每條路線的內容均使用ng-content針對特定注射點。我可以使用<ng-container *ngComponentOutlet="ResponsiveComponent;"></ng-container>換出父組件。問題是我的路由生成的內容沒有被注入到每個父組件的目標位置ng-content

這是用於交換父組件的代碼。 AppPageComponent延伸ResponsiveComponent,它基於媒體查詢監視設備寬度。

@Component({ 
    selector: 'app-page', 
    template: ` 
     <ng-container *ngComponentOutlet="ResponsivePageComponent;"></ng-container>  
    `, 
    styleUrls: [ './page.component.scss'], 
    providers: [] 
}) 
export class AppPageComponent extends ResponsiveComponent implements OnInit, OnDestroy 
{ 
    ResponsivePageComponent; 

    constructor(
     injector: Injector, 
     zone: NgZone) 
    { 
     super(zone);  
    } 

    ngOnInit() 
    {  
     this.IsSmallEnvironmentStream 
      .subscribe 
      (
       (isSmall: boolean) => 
       { 
        if (isSmall) 
        { 
         this.ResponsivePageComponent= AppPageSmallComponent; 
        }     
       }, 
       (err) => { }, 
       () => {} 
      ); 

     this.IsMediumEnvironmentStream 
      .subscribe 
      (
       (isMedium: boolean) => 
       { 
        if (isMedium) 
        { 
         this.ResponsivePageComponent = AppPageMediumComponent; 
        }     
       }, 
       (err) => { }, 
       () => {} 
      ); 

     this.IsLargeEnvironmentStream 
      .subscribe 
      (
       (isLarge: boolean) => 
       { 
        if (isLarge) 
        { 
         this.ResponsivePageComponent = AppPageLargeComponent; 
        }     
       }, 
       (err) => { }, 
       () => {} 
      );  
    } 
} 

角度對NgComponentOutlet文檔顯示可以針對可投影節點的可選列表插入內容(NG-內容)佔位符。我遵循了Angular的文檔中的例子,並且能夠將文本節點注入到我父組件中的不同ng-content佔位符中,並且它適用於我的小/中/大父組件。

如何從我的路線獲取內容以注入這些不同的佔位符?看起來我真的很接近這個工作。我可以將靜態內容注入到參與者中,但我無法弄清楚如何將路由生成的內容注入佔位符。

更新 - 2017年7月25日

我創造了這個plunker demonstrating this scenariohttps://plnkr.co/edit/hOwRX1Ml6gX0S8em6nd5?p=preview)。

在重載程序中,page-wrapper組件使用ngComponentOutlet根據設備寬度加載小/中/大組件。頁面包裝器組件將靜態內容注入到每個小/中/大組件的佔位符中,而不是從第1頁注入路由內容。

我的目標是擁有一個頁面包裝器組件來處理響應式需求,如不同的導航元素小/中/大型響應式設計。

那麼,怎樣才能「靜態」的內容被注入到動態加載組件的ng-content佔位但路線生成的內容不被注入ng-content佔位符?

感謝您的幫助。

+0

'router-outlet'沒有在HTML中定義。 – Aravind

+0

你好!你能說什麼不起作用嗎?路由3不會通過選擇器傳輸ng-content?如果你將你的代碼中的代碼減少到最小程度,那將會很好。 – yurzui

+0

@yurzui - 感謝您的反饋。我已經澄清了重拳。這名運動員現在只有2條路線。路由1使用「響應」父組件,其內容不會注入到父節點的內容佔位符中。路線2使用「無響應」的父組件,其內容確實被嵌入到其母公司的ng內容佔位符中。 –

回答

4

1)您可以在PageWrapperComponent定義ng-content的地方,他們transclude動態組件

頁面wrapper.component.html

<ng-container *ngComponentOutlet="ResponsiveComponent; 
           content: [[el1],[el2]]"></ng-container> 
<div #el1> 
    <ng-content></ng-content> 
</div> 
<div #el2> 
    <ng-content select="[named-injection-point]"></ng-content> 
</div> 

Plunker Example

2)你也可以在父組件中使用模板引用變量(或自定義指令),並在中使用得到transcluding部分。

頁面one.component.html

<page-wrapper> 
    <section #mainPoint> 
     <h2>Page One</h2> 
    </section> 
    <section #namedPoint> 
     <h3>Page One Content:</h3> 
     <p>Lorem ipsum dolor.</p> 
    </section> 
</page-wrapper> 

頁面wrapper.component.ts

@ContentChild('mainPoint') mainPoint; 
@ContentChild('namedPoint') namedPoint; 

頁面wrapper.component.html

<ng-container *ngComponentOutlet="ResponsiveComponent; 
     content: [[mainPoint.nativeElement],[namedPoint.nativeElement]]"></ng-container> 

Plunker Example

+0

謝謝!!!!!!這正是我需要的。 –

+0

ngComponentOutlet指令使用@ContentChild nativeElements數組將內容傳遞給動態加載的組件。我必須按照特定順序放入數組,才能將注入的內容轉換爲正確的ng內容。有沒有一種方法可以保證數組中的項目被注入到動態加載的組件中正確的ng內容中? –

+0

@Tom Schreck你必須保持秩序。 https://stackoverflow.com/questions/44284026/creating-a-angular2-component-with-ng-content-dynamically/44289996#44289996你應該按照相同的順序傳遞數組'ng-content'出現在模板中。角在這種情況下不使用'select'屬性,只有指數 – yurzui

0

這是你所期待的嗎?我只測試了中型組件BTW ...

https://plnkr.co/edit/JgCQb4JVSHnHCueamerV?p=preview

我想這是因爲你試圖用NG-內容設置在不直接從具有內容的組件調用的組件你想注入。我所做的只是在PageWrapperComponent上添加一個ng-content,並使用它自己的選擇器,然後將它傳遞給下一個組件,並使用期望的選擇器。

PageWrapperComponent ...

<ng-content select="[injected-content]" named-injection-point></ng-content> 

然後在PageOneComponent使用了新的選擇包裝期待

<section injected-content> 
    <h3>Page One Content:</h3> 
    <p>Lorem ipsum ...</p> 
</section>  

現在我可以看到頁面下吉米和弗雷德一個內容,當我點擊第1頁鏈接。

+0

感謝您的回覆。我需要用Jimmy和Fred的路線數據替換Jimmy和Fred,而不是將路線數據放在Jimmy和Fred的下方。我只是使用Jimmy和Fred作爲放入動態組件內容的示例。我無法弄清楚如何獲取路線數據。 –