Angular pass multiple templates to Component

后端 未结 2 1779
孤城傲影
孤城傲影 2020-12-25 09:20

I\'m trying to create a component that accepts multiple templates as inputs. This is the example I have:

@Component({
    selector: \'data-list\',
    styles         


        
2条回答
  •  攒了一身酷
    2020-12-25 09:38

    I solved this same issue by using the string selector available to the ContentChild decorator.

    You will need to specify template variables when using your data-list component:

    
        
            

    {{ item.header }}

    {{ item.content }}

    {{ item.header }}

    {{ item.content }}

    Then, inside your data-list component class, assign the template variables to local variables on the component:

    @Input() itemsData: any[];
    @ContentChild('firstItemTemplate') firstItemTemplate: TemplateRef;
    @ContentChild('standardTemplate') standardTemplate: TemplateRef;
    

    After this, you'll be able to render the passed-in templates from your data-list component.

    @Component({
        selector: 'data-list',
        styles: [
            require('./data-list.component.scss')
        ],
        template: `
            
                
                
            
        `
    })
    

提交回复
热议问题