angular - Angular2 alt bileşenler


Aşağıdaki bileşeni oluşturmak istiyorum:

<my-component [attr1]="attr1" [attr2]="attr2">
</my-component>  

Bu iyi, ama ben bu bileşende her tür şablonu render etmek istiyorum, bu yüzden buna benzer bir şeye ihtiyacım var:

<my-component [attr1]="attr1" [attr2]="attr2">
    <my-component-template>
        // Here comes the template for my-component component
    </my-component-template>
</my-component>  

Bunu nasıl yapacağımıza dair herhangi bir örnek var mı? Hiçbir referans bulamadım.

Cevap #1

Sanırım <ng-content> arıyorsunuz veya bileşenleri ViewContainerRef ve ComponentFactoryResolver kullanarak ekliyorsunuz. Resmi belgelerde bunlardan hiç faydalı bir örnek bulamadım aslında...

Şuna bakın <ng-content> kullanımı:

  • https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html

  • https://angular.io/docs/ts/latest/cookbook/dependency-injection.html

Bileşenleri dinamik olarak eklemeyi görün:

  • Kullanıcı tarafından tıklanarak seçilen bileşenlerle Angular 2 dinamik sekmeler

  • https://angular.io/docs/ts/latest/api/core/index/ViewContainerRef-class.html#!#createComponent-anchor

  • https://angular.io/docs/ts/latest/api/core/index/ComponentFactoryResolver-class.html