Angular 2 understand using ViewContainerRef with TemplateRef

I am implementing a structural directive in Angular2 . In the docs, I saw what I needed to enter TemplateRefin order to get the template element and ViewContainerRef.

class TestDirective {
   constructor( private templateRef : TemplateRef<any>, private viewContainer : ViewContainerRef ) {
     // this.viewContainer.createEmbeddedView(this.templateRef);
   }   
}

<template [ngIf]="condition">
  <p>
    Our heroes are true!
  </p>
</template>

In this case, I do not understand which element is ViewContainerRef?

+4
source share
1 answer

If you enter ViewContainerRef, the element is the host element TestDirective. If you use @ViewChild(templateVarNameOrType)or @ContentChild(templateVarNameOrType), then matches the element where templateVarNameOrType.

, this.viewContainer.createEmbeddedView() this.viewContainer.createComponent() ViewContainerRef, , .

+2

Source: https://habr.com/ru/post/1658748/


All Articles