2017-03-31 2 views
2

구성 요소 내에 모든 템플릿 (TemplateRef)의 컬렉션을 얻으려면 어떻게해야합니까? 그것은 ViewChild와 잘 작동하지만 ViewChildren은 정의되어 있지 않습니다 ...<template> 요소에 대해 @ViewChildren (TemplateRef)이 정의되지 않았습니다.

나는이 question에서 해결책을 사용합니다. Plunker 전체 예제가 있습니다.

@Component({ 
    selector: 'my-app', 
    template: ` 
      <div> 
       <template #model>...</template> 
       <template #color>...</template> 
      </div>` 
    }) 
    export class App { 
    @ViewChild('model') model; 
    @ViewChild('color') color; 
    @ViewChildren(TemplateRef) templates: QueryList<TemplateRef>; 

    ngAfterContentInit() { 
    console.log(this.templates); // undefined 
    console.log(this.model); // TemplateRef_ {...} 
    } 
} 

가 나는 열에 대한 템플릿을 정의하는 것이 가능 그리드 구성 요소에 대한 templates이 필요합니다. 불행히도 ng 콘텐츠 doesn't support 동적 투영, 그래서 그것을 템플릿으로 달성하려고 해요.

답변

1

console.log('child', this.templates);ngAfterViewInit()으로 옮기면 작동합니다.

ngAfterViewInit() { 
    console.log('child', this.templates); 
} 

나는 ngAfterContentInit()에서도 작동 할 것으로 예상했다. 이 각도 버전 ngAfterContentInit()ngAfterViewInit() 이전에 실행됩니다. 나는 이것이 이전의 다른 길이었다라고 확신했다.

Plunker example