当将ViewContainerRef注入指令时,它绑定了什么元素



当将ViewContainerRef注入指令时,它绑定到什么元素?

例如,如果我们有一个模板:

template `<div><span vcdirective></span></div>`

vcdirective的构造函数看起来像:

  constructor(vc: ViewContainerRef) {
  }

vc:ViewContainerRef绑定到span元素的元素?

是的,它将绑定到跨度元素。如果在构造函数中安装VC,则会看到ViewContainerRef对象,检查其元素属性,您会在其中找到 span> span 。但是,当您将视图连接到容器中时,它将附加在范围旁边,而不是被插入其中并将跨度悬挂。

是的,它的限制为跨度。ViewContainerRef代表一个可以连接一个或多个视图的容器。我们可以使用其方法,例如createEmbeddedView()createComponent()ViewContainerRef用于构建动态组件

请检查一下以更好地理解。它是一个可以附加到另一个节能的占位符。检查https://netbasal.com/angular-2-underding-viewcontainerref-acc183f3b682

类似:

    @Component({
  selector: 'vcr',
  template: `
    <template #tpl>
      <h1>ViewContainerRef</h1>
    </template>
  `,
})
export class VcrComponent {
  @ViewChild('tpl') tpl;
  constructor(private _vcr: ViewContainerRef) {
  }
  ngAfterViewInit() {
    this._vcr.createEmbeddedView(this.tpl);
  }
}

最新更新