Angular2: replace host element with component's template

前端 未结 5 1713
别跟我提以往
别跟我提以往 2021-01-30 17:23

I\'m new to angular in general and to angular2 specifically. I\'m trying to write a container component, which should have child components in it.

5条回答
  •  天涯浪人
    2021-01-30 18:05

    Finally I found solution: injecting ElementRef to MyItem and using its nativeElement.innerHTML:

    MyList:

    import { Component, ContentChildren, QueryList } from 'angular2/core'
    import { MyItem } from './myitem'
    
    @Component({
      selector: 'my-list',
      template: `
        
    ` }) export class MyList { @ContentChildren(MyItem) items: QueryList }

    MyItem:

    import { Directive, Inject, ElementRef } from 'angular2/core'
    
    @Directive({selector: 'my-item'})
    export class MyItem {
      constructor(@Inject(ElementRef) element: ElementRef) {
        this.innerHTML = element.nativeElement.innerHTML
      }
    }
    

    Working plunk is here

提交回复
热议问题