ViewContainerRef

Represents a container where one or more Views can be attached.

代表一个容器,可以在里面附加一个或多个视图。

abstract class ViewContainerRef { abstract get element: ElementRef abstract get injector: Injector abstract get parentInjector: Injector abstract get length: number abstract clear(): void abstract get(index: number): ViewRef | null abstract createEmbeddedView<C>(templateRef: TemplateRef<C>, context?: C, index?: number): EmbeddedViewRef<C> abstract createComponent<C>(componentFactory: ComponentFactory<C>, index?: number, injector?: Injector, projectableNodes?: any[][], ngModule?: NgModuleRef<any>): ComponentRef<C> abstract insert(viewRef: ViewRef, index?: number): ViewRef abstract move(viewRef: ViewRef, currentIndex: number): ViewRef abstract indexOf(viewRef: ViewRef): number abstract remove(index?: number): void abstract detach(index?: number): ViewRef | null }

说明

The container can contain two kinds of Views. Host Views, created by instantiating a Componentvia createComponent, and Embedded Views, created by instantiating an Embedded Template via createEmbeddedView.

此容器可以包含两种不同的视图。宿主视图,在通过 createComponent 实例化 Component时创建; 内嵌视图,在通过 createEmbeddedView 实例化 内嵌模板 时创建。

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.

ViewContainer(视图容器)在父容器视图中的位置由锚点元素 element 决定。 每个 ViewContainer 只能有一个锚点元素,而且每个锚点元素中只能有一个 ViewContainer。

Root elements of Views attached to this container become siblings of the Anchor Element in the Rendered View.

在渲染出的视图中,这些附加到该容器中的视图的根元素会成为锚点元素的兄弟。

To access a ViewContainerRef of an Element, you can either place a Directiveinjected with ViewContainerRef on the Element, or you obtain it via a ViewChildquery.

要想访问元素的 ViewContainerRef,你可以放一个 Directive,并把该元素的 ViewContainerRef 注入进去, 也可以通过 ViewChild查询来取到它。

属性

属性名类型说明
element只读

Anchor element that specifies the location of this container in the containing View.

锚点元素用来指定本容器在父容器视图中的位置。

injector只读
parentInjector只读
length只读

Returns the number of Views currently attached to this container.

返回目前附加到本容器的视图的数量。

方法

Destroys all Views in this container.

销毁本容器中的所有视图。

abstract clear(): void

参数

没有参数。

返回值

void

Returns the ViewReffor the View located in this container at the specified index.

返回本容器中指定序号的视图的 ViewRef

abstract get(index: number): ViewRef | null

参数

index

Type: number.

返回值

ViewRef | null

Instantiates an Embedded View based on the `templateRef` and inserts it into this container at the specified index.

根据 `templateRef` 实例化一个内嵌视图,并把它插入在本容器的指定 index 处。

abstract createEmbeddedView<C>(templateRef: TemplateRef<C>, context?: C, index?: number): EmbeddedViewRef<C>

参数

templateRef

Type: TemplateRef.

context

Type: C.

可选. 默认值是 undefined.

index

Type: number.

可选. 默认值是 undefined.

返回值

EmbeddedViewRef<C>

If index is not specified, the new View will be inserted as the last View in the container.

如果没有指定 index,则这个新视图将会被插入在本容器的末尾处。

Returns the ViewReffor the newly created View.

返回新建视图的 ViewRef

Instantiates a single Componentand inserts its Host View into this container at the specified index.

实例化一个 Component并把它的宿主视图插入到本容器的指定 index 处。

abstract createComponent<C>(componentFactory: ComponentFactory<C>, index?: number, injector?: Injector, projectableNodes?: any[][], ngModule?: NgModuleRef<any>): ComponentRef<C>

参数

componentFactory

Type: ComponentFactory.

index

Type: number.

可选. 默认值是 undefined.

injector

Type: Injector.

可选. 默认值是 undefined.

projectableNodes

Type: any[][].

可选. 默认值是 undefined.

ngModule

Type: NgModuleRef.

可选. 默认值是 undefined.

返回值

ComponentRef<C>

The component is instantiated using its ComponentFactorywhich can be obtained via resolveComponentFactory.

该组件使用它的 ComponentFactory进行实例化。ComponentFactory 可以通过 resolveComponentFactory 拿到。

If index is not specified, the new View will be inserted as the last View in the container.

如果没有指定 index 则这个新视图将会被插入在本容器的末尾处。

You can optionally specify the Injectorthat will be used as parent for the Component.

你还可以指定一个可选的 Injector,它将被用作本组件的父注入器。

Returns the ComponentRefof the Host View created for the newly instantiated Component.

返回新实例化的组件的宿主视图的 ComponentRef

Inserts a View identified by a ViewRefinto the container at the specified index.

把一个由 ViewRef标识的视图插入到容器中的指定 index 处。

abstract insert(viewRef: ViewRef, index?: number): ViewRef

参数

viewRef

Type: ViewRef.

index

Type: number.

可选. 默认值是 undefined.

返回值

ViewRef

If index is not specified, the new View will be inserted as the last View in the container.

如果没有指定 index 则这个新视图将会被插入在本容器的末尾处。

Returns the inserted ViewRef.

返回所插入的 ViewRef

Moves a View identified by a ViewRefinto the container at the specified index.

把一个由 ViewRef标记的视图移入容器中指定的 index 处。

abstract move(viewRef: ViewRef, currentIndex: number): ViewRef

参数

viewRef

Type: ViewRef.

currentIndex

Type: number.

返回值

ViewRef

Returns the inserted ViewRef.

返回所插入的 ViewRef

Returns the index of the View, specified via ViewRef, within the current container or -1 if this container doesn't contain the View.

返回本视图在其容器中的索引,如果没找到,则返回 -1

abstract indexOf(viewRef: ViewRef): number

参数

viewRef

Type: ViewRef.

返回值

number

Destroys a View attached to this container at the specified index.

销毁一个位于容器中指定 index 处的视图。

abstract remove(index?: number): void

参数

index

Type: number.

可选. 默认值是 undefined.

返回值

void

If index is not specified, the last View in the container will be removed.

如果不指定 index,则移除容器中的最后一个视图。

Use along with insert to move a View within the current container.

insert 一起使用,把某个视图移入当前容器。

abstract detach(index?: number): ViewRef | null

参数

index

Type: number.

可选. 默认值是 undefined.

返回值

ViewRef | null

If the index param is omitted, the last ViewRefis detached.

如果省略 index 参数,则拆出最后一个 ViewRef