ViewContainerRef 动态创建视图

Angular DOM 操作

相关的APIs和类:

  • 查询DOM节点
    • template variable ref: 模版变量引用,相当于react中的ref
    • ViewChild: 查询DOM,返回单个元素引用
    • ViewChildren: 返回一个QueryList对象,包含一系列元素
  • ElementRef: 元素引用
    • 查询的方式获取,比如 @ViewChild('myInput') inputElm: ElementRef
    • 依赖注入的方式,获取宿主元素,比如 constructor(private elem: ElementRef){}
  • TemplateRef: 模板引用
    • 查询的方式, 比如 <ng-template #tpl></ng-template>
  • ViewContainerRef: 视图容器,包含创建angular视图的方法和操作视图的apis
  • ViewRef: 视图引用,angular最小的UI单元,创建的视图的返回类型就是ViewRef
  • angular中的2种类型的视图
    • 插入式视图Embedded Views
    • 宿主视图 component instance views,即组件实例视图
  • EmbeddedViewRef: 插入式视图引用,上面创建插入式视图防护的类型
  • ComponentRef<C>: 组件视图引用,创建hostView时返回的类型 <C> 表示组件名
Angular+DOM操作.png

示例

创建视图并插入

import {
  Component,
  ViewChild,
  TemplateRef,
  ViewContainerRef,
  ViewRef,
  AfterViewInit
} from '@angular/core';

@Component({
  selector: 'app-sample',
  template: `
    <span>我是第一个span标签</span>
    <ng-container #vc></ng-container>
    <span>我是第二个span标签</span>
    <ng-template #tpl>
      <div>我是模版里面的div标签</div>
    </ng-template>
  `
})
export class SampleComponent implements AfterViewInit {
  // 查询元素, {read: ViewContainerRef} 不能省略,因为angular无法推断出它是一个容器
  @ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
  // {read: TemplateRef} 可以省略
  @ViewChild('tpl') tpl: TemplateRef<any>;

  constructor() {}

  ngAfterViewInit() {
    // 创建一个插入式视图, 一般插入式视图都对应的是模版视图
    const tplView: ViewRef = this.tpl.createEmbeddedView(null);

    // 插入到容器当中 使用视图容器操作视图的方法insert
    this.vc.insert(tplView);
    
  }
}

最后得到的结果是

<app-sample>
    <span>我是第一个span标签</span>
    <!--template bindings={}-->
    <div>我是模版里面的div标签</div>
    <span>我是第二个span标签</span>
    <!--template bindings={}-->
</app-sample>

可以看出 ng-containerng-template 最后编译后都变为了注释

<!--template bindings={}-->

使用 ngTemplateOutlet 指令

angular为创建插入式视图提供了 ngTemplateOutlet 指令,这个和 router-outlet 功能类似,为模版提供了一个进入的入口,上面的例子可以改写为

import {
  Component,
} from '@angular/core';

@Component({
  selector: 'app-sample',
  template: `
    <span>我是第一个span标签</span>
    <ng-container [ngTemplateOutlet]="tpl"></ng-container>
    <span>我是第二个span标签</span>
    <ng-template #tpl>
      <div>我是模版里面的div标签</div>
    </ng-template>
  `
})
export class SampleComponent {
}

可以看出这种十分的方便,在饿了吗angular tooltip组件中就使用到了这个指令


@Component({
  selector: 'el-tooltip',
  template: `
    <div style="position: relative; display: inline-block;">
      <div [class]="'el-tooltip__popper is-' + effect + ' ' + popperClass"
        style="left: -20000px; top: 0; position: absolute;"
        [@fadeAnimation]="!showPopper" [attr.x-placement]="xPlacement" #popperContent>
        <div x-arrow class="popper__arrow" [hidden]="!visibleArrow"></div>
        <ng-template [ngTemplateOutlet]="tip"></ng-template>  # 此处使用到了ngTemplateOutlet指令
      </div>
      <ng-content></ng-content>
    </div>
  `,
  animations: [fadeAnimation],
})
export class ElTooltip implements AfterContentInit {
  @ContentChild('tip') tip: TemplateRef<any>
}

使用时:

<el-tooltip>
  <ng-template #tip>我是将要插入的模版内容<ng-template>
</el-tooltip>

ngComponentOutlet

这个指令和上面的 ngTemplateOutlet类似,但是它将创建一个 host view(组件的实例),而不是插入式视图,使用方式

<ng-container *ngComponentOutlet="ColorComponent"></ng-container>

ViewContainerRef源码

import { Injector } from '../di/injector';
import { ComponentFactory, ComponentRef } from './component_factory';
import { ElementRef } from './element_ref';
import { NgModuleRef } from './ng_module_factory';
import { TemplateRef } from './template_ref';
import { EmbeddedViewRef, ViewRef } from './view_ref';
/**
 * Represents a container where one or more Views can be attached.
 * 表示能够被一个或者多个视图附着的容器
 * The container can contain two kinds of Views. Host Views, created by instantiating a
 * {@link Component} via {@link #createComponent}, and Embedded Views, created by instantiating an
 * {@link TemplateRef Embedded Template} via {@link #createEmbeddedView}.
 * 容器能够包含2种类型的视图: 宿主视图(组件实例) 和 插入式视图(使用模版创建的视图)
 * The location of the View Container within the containing View is specified by the Anchor
 * `element`. Each View Container can have only one Anchor Element and each Anchor Element can only
 * have a single View Container.
 *
 * Root elements of Views attached to this container become siblings of the Anchor Element in
 * the Rendered View.
 * 插入的视图的根元素会成为视图容器的兄弟节点,而不是之间插入到容器中,这点和router-outlet插入组件的方式一致
 *
 * To access a `ViewContainerRef` of an Element, you can either place a {@link Directive} injected
 * with `ViewContainerRef` on the Element, or you obtain it via a {@link ViewChild} query.
 * 可以通过注入的方式范围viewContainerRef 和 通过 ViewChild 查询的方式获取viewContainerRef
 * @stable
 */
export declare abstract class ViewContainerRef {
    /**
     * Anchor element that specifies the location of this container in the containing View.
     * <!-- TODO: rename to anchorElement -->
     */
    readonly abstract element: ElementRef; 
    readonly abstract injector: Injector;         // 注入器, 用于动态创建组件中
    readonly abstract parentInjector: Injector;   // 父注入器, 如果组件自身没有提供注入器,使用父注入器
    /**
     * Destroys all Views in this container. 销毁容器内的所有视图
     */
    abstract clear(): void;
    /**
     * Returns the {@link ViewRef} for the View located in this container at the specified index.
     * 返回视图引用索引
     */
    abstract get(index: number): ViewRef | null;

    /**
     *  获取视图容器的数量
     * Returns the number of Views currently attached to this container.
     */
    readonly abstract length: number;

    /**
     * 实例化一个插入式视图,可以插入到指定的索引位置,如果不指定索引,将放到最后面
     * Instantiates an Embedded View based on the {@link TemplateRef `templateRef`} and inserts it
     * into this container at the specified `index`.
     * 
     * If `index` is not specified, the new View will be inserted as the last View in the container.
     *
     * Returns the {@link ViewRef} for the newly created View. 返回一个视图引用
     */
    abstract createEmbeddedView<C>(templateRef: TemplateRef<C>, context?: C, index?: number): EmbeddedViewRef<C>;

    /**
     * 实例化单个组件,插入到宿主视图中,可以插入到指定的索引位置,如果不指定索引,将放到最后面
     * Instantiates a single {@link Component} and inserts its Host View into this container at the
     * specified `index`.
     * 
     * The component is instantiated using its {@link ComponentFactory} which can be
     * obtained via {@link ComponentFactoryResolver#resolveComponentFactory}.
     * 组件通过 ComponentFactory 实例化, 而组件工厂可以通过 ComponentFactoryResolver来创建
     * If `index` is not specified, the new View will be inserted as the last View in the container.
     *
     * You can optionally specify the {@link Injector} that will be used as parent for the Component.
     *
     * Returns the {@link ComponentRef} of the Host View created for the newly instantiated Component.
     */
    abstract createComponent<C>(componentFactory: ComponentFactory<C>, index?: number, injector?: Injector, projectableNodes?: any[][], ngModule?: NgModuleRef<any>): ComponentRef<C>;

    /**
     * 插入视图
     * Inserts a View identified by a {@link ViewRef} into the container at the specified `index`.
     * 
     * If `index` is not specified, the new View will be inserted as the last View in the container.
     *
     * Returns the inserted {@link ViewRef}.
     */
    abstract insert(viewRef: ViewRef, index?: number): ViewRef;

    /**
     * 依据索引移动视图
     * Moves a View identified by a {@link ViewRef} into the container at the specified `index`.
     *
     * Returns the inserted {@link ViewRef}.
     */
    abstract move(viewRef: ViewRef, currentIndex: number): ViewRef;

    /**
     * 返回视图的索引位置
     * Returns the index of the View, specified via {@link ViewRef}, within the current container or
     * `-1` if this container doesn't contain the View.
     */
    abstract indexOf(viewRef: ViewRef): number;

    /**
     * 移除视图
     * Destroys a View attached to this container at the specified `index`.
     *
     * If `index` is not specified, the last View in the container will be removed.
     */
    abstract remove(index?: number): void;

    /**
     * 将视图从当前容器中分离
     * Use along with {@link #insert} to move a View within the current container.
     *
     * If the `index` param is omitted, the last {@link ViewRef} is detached.
     */
    abstract detach(index?: number): ViewRef | null;
}

2种视图

模版视图

也称之为插入式视图

<ng-template #tpl></ng-template>
<ng-container #vc><ng-container>

class SampleComponent implments AfterViewInit {
  @ViewChild('tpl') tpl: Template<any>;
  @ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
  ngAfterViewInit() {
    let embeddedView: ViewRef = this.tpl.createEmbeddedView(null);
    this.vc.insert(embeddedView);
  }
}

宿主视图

和组件相关

<ng-container #vc><ng-container>

class SampleComponent implments AfterViewInit {
  @ViewChild('tpl') tpl: Template<any>;
  @ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
  componentRef: ComponentRef<ColorComponent>;

  constructor(private injector: Injector, provate cfr: ComponentFactoryResolver) {}
  ngAfterViewInit() {
    const factory = this.cfr.rosolveComponentFactory(ColorComponent); // 创建组件工厂
    this.componentRef = this.vc.createComponent(factory); // 创建组件引用
    // this.componentRef = factory.create(this.injector); // 创建注入器
    // let view: ViewRef = componentRef.hostView;     // 创建宿主视图
    // this.vc.insert(view);
  }
}

最后动态创建的组件需要添加到 entryComponent

示例:

文章参考:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335