Angular DOM 操做
相关的APIs和类:javascript
- 查询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视图的方法和操做视图的apisViewRef
: 视图引用,angular最小的UI单元,建立的视图的返回类型就是ViewRef- angular中的2种类型的视图
- 插入式视图
Embedded Views
- 宿主视图
component instance views
,即组件实例视图
- 插入式视图
EmbeddedViewRef
: 插入式视图引用,上面建立插入式视图防御的类型ComponentRef<C>
: 组件视图引用,建立hostView时返回的类型<C>
表示组件名
示例
建立视图并插入php
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); } }
最后获得的结果是html
<app-sample> <span>我是第一个span标签</span> <!--template bindings={}--> <div>我是模版里面的div标签</div> <span>我是第二个span标签</span> <!--template bindings={}--> </app-sample>
能够看出 ng-container
和 ng-template
最后编译后都变为了注释java
<!--template bindings={}-->
使用 ngTemplateOutlet 指令
angular为建立插入式视图提供了 ngTemplateOutlet
指令,这个和 router-outlet
功能相似,为模版提供了一个进入的入口,上面的例子能够改写为react
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组件中就使用到了这个指令api
@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> }
使用时:ruby
<el-tooltip> <ng-template #tip>我是将要插入的模版内容<ng-template> </el-tooltip>
ngComponentOutlet
这个指令和上面的 ngTemplateOutlet
相似,可是它将建立一个 host view
(组件的实例),而不是插入式视图,使用方式app
<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种视图
模版视图dom
也称之为插入式视图ide
<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
中
示例:
文章参考: