Angular的ViewContainerRef、TemplateRef、EmbeddedViewRef

ViewContainerRef 可以包含一个或者多个视图的容器
TemplateRef 内嵌模板,可用来创建内嵌视图
EmbeddedViewRef 内嵌视图

假设ts中注入private viewContainerRef:ViewContainerRef ,private templateRef:TemplateRef 两个服务

在ViewContainerRef 和TemplateRef 中都有createEmbeddedView创建视图的方法,所以往ViewContainerRef 容器插入视图的时候可以有两种方式:

1this.viewContainerRef.createEmbeddedView(this.tmplateRef,context?)
第一种方法由ViewContainerRef 容器创建EmbeddedViewRef视图,然后插入TemplateRef模板,context是设置的模板内容。

2this.viewContainerRef.inster(this.templateRef.createEmbeddedView(context?),index?)
第二种方法由TemplateRef  模板创建EmbeddedViewRef 视图,ViewContainerRef 容器调用自身的inster 方法直接插入,index为视图插入容器的位置索引,索引从0开始

代码演示

html代码

<div class="box" #box>
  <button class="btn btn-primary mr-1" (click)="insert(secondTpl)">insert second</button>
  <button class="btn btn-primary mr-1" (click)="insert(thirdTpl)">insert third</button>
  <button class="btn btn-primary mr-1" (click)="insert(fourthTpl)">insert fourth</button>
  <button class="btn btn-primary mr-1" (click)="insertAll()">insert all</button>
  <button class="btn btn-secondary mr-1" (click)="insertFree()">insert free</button>
  <button class="btn btn-info mr-1" (click)="getOne()">get one</button>
  <button class="btn btn-success mr-1" (click)="move()">move free</button>
  <button class="btn btn-success mr-1" (click)="move2To4()">把第二个移动到第四个位置上</button>
  <button class="btn btn-success" (click)="move2ToOther()">把第二个移动到其他容器中</button>
  <p>长度:{{ firstContain?.length }}</p>
</div>
<ng-template #firstTpl>
  <p>first tpl content</p>
</ng-template>


<ng-template #secondTpl>
  <p>第二段template</p>
</ng-template>

<ng-template #thirdTpl>
  <p>第三段template</p>
</ng-template>

<ng-template #fourthTpl>
  <p>第四段template</p>
</ng-template>

<ng-template #freeTpl>
  <p>自由的template</p>
</ng-template>

<p>
  first container:
  <ng-container #firstContainer></ng-container>
</p>

<hr>

<p>
  second container:
  <ng-container #secondContainer></ng-container>
</p>

ts代码

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

@Component({
  selector: 'app-tpl-container',
  templateUrl: './tpl-container.component.html',
  styleUrls: ['./tpl-container.component.scss']
})
export class TplContainerComponent implements OnInit,AfterViewInit {

  @ViewChild('firstTpl', {read: TemplateRef}) readonly firstTpl: TemplateRef<any>
  @ViewChild('secondTpl', {read: TemplateRef}) readonly secondTpl: TemplateRef<any>
  @ViewChild('thirdTpl', {read: TemplateRef}) readonly thirdTpl: TemplateRef<any>
  @ViewChild('fourthTpl', {read: TemplateRef}) readonly fourthTpl: TemplateRef<any>
  @ViewChild('freeTpl', {read: TemplateRef}) readonly freeTpl: TemplateRef<any>

  @ViewChild('firstContainer', {read: ViewContainerRef, static: true}) firstContain: ViewContainerRef
  @ViewChild('secondContainer', {read: ViewContainerRef, static: true}) secondContain: ViewContainerRef

  private freeView: EmbeddedViewRef<any>

  constructor() {
  }

  ngOnInit(): void {
  }

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.firstContain.createEmbeddedView(this.firstTpl);
    },0);
    this.freeView = this.freeTpl.createEmbeddedView({$implicit:'defaultValue',free:'free'});
  }

  insert(tpl: TemplateRef<any>) {
    this.firstContain.insert(tpl.createEmbeddedView(null));
  }

  insertAll() {
    [this.secondTpl, this.thirdTpl, this.fourthTpl].forEach(tpl => {
      this.firstContain.insert(tpl.createEmbeddedView(null));
    });
  }

  insertFree() {
    this.firstContain.insert(this.freeView);
  }

  getOne() {
    let view = this.firstContain.get(2);
    console.log("view", view);
  }

  move() {
    this.firstContain.move(this.freeView, 1);
  }

  move2To4() {
    let view = this.firstContain.detach(1);
    this.firstContain.insert(view, 3);
  }

  move2ToOther() {
    let view = this.firstContain.detach(1);
    this.secondContain.insert(view);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值