Angular视图相关

ElementRef

// html 
<p #p>some text</p>
// js
@ViewChild('p') pRef: ElementRef
复制代码

可经过pRef.nativeElement拿到原生节点html

TemplateRef

// html
<ng-template #container>some text</ng-template>
// js
@ViewChild('container') conRef: TemplateRef
复制代码

未渲染前以注释节点占位bash

ViewContainerRef

// html
<div #div>some text</div>
// js
@ViewChild('div', {read: ViewContainerRef}) divVcRef: ViewContainerRef;
复制代码

能够将其理解为一块视图容器,经过下面两种方法能够往视图中添加DOMapp

  1. CreateEmbeddedView,入参类型TemplateRef
  2. CreateComponent, 入参类型ComponentFactory

默认行为===appendChild,修改index可改变插入位置ui

生成factory:this

import { ComponentFactoryResolver } from '@angular/core'
import { ComponentA } from 'xxx'
...

insert() {
    const factory = this.comFR.resolveComponentFactory(ComponentA)
    this.divVcRef.createComponent(factory)
}
复制代码

ComponentRef

由Factory生成的组件引用,常见的应用是建立Modalspa

const factory = this.cfr.resolveComponentFactory(ComfirmModalComponent);
const componentRef = factory.create(this.injector)
this.instanceRef = componentRef;
componentRef.instance.isShow = true;
this.appRef.attachView(componentRef.hostView);
document.body.appendChild( (componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0])
复制代码

内容投影

与Vue的Slot相似,可插入DOM or Component,支持多插槽code

// ChildComponent
<div class="panel panel-primary">
  <div class="panel-heading">
      <ng-content select="h3"></ng-content>
  </div>
  <div class="panel-body">
      <ng-content select="child2"></ng-content>
  </div>
  <div class="panel-footer">
      <ng-content select="p"></ng-content>
  </div>
</div>

// ParentComponent
<child>
    <h3>这是父层投影进来的内容</h3>
    <child (sayhello)="doSomething()"></child>
    <p>这是底部内容</p>
</child>
复制代码

控制投影进来的组件

@ContentChild(Child2) child2Com: Child2Component
复制代码
相关文章
相关标签/搜索