ViewContainerRef
表示可以将一个或多个视图附着到组件中的容器。
Represents a container where one or more views can be attached to a component.
abstract class ViewContainerRef {
abstract element: ElementRef
abstract injector: Injector
abstract parentInjector: Injector
abstract 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
}
参见
说明
可以包含宿主视图(当用 createComponent()
方法实例化组件时创建)和内嵌视图(当用 createEmbeddedView()
方法实例化 TemplateRef
时创建)。
Can contain host views (created by instantiating a component with the createComponent()
method), and embedded views (created by instantiating a TemplateRef
with the createEmbeddedView()
method).
视图容器的实例还可以包含其它视图容器,以创建层次化视图。
A view container instance can contain other view containers, creating a view hierarchy.
属性
属性 | 说明 |
---|---|
abstract element: ElementRef | 只读 锚点元素用来指定本容器在父容器视图中的位置。 每个视图容器都只能有一个锚点元素,每个锚点元素也只能属于一个视图容器。 Anchor element that specifies the location of this container in the containing view. Each view container can have only one anchor element, and each anchor element can have only a single view container. 视图的根元素会附着到该容器上,在渲染好的视图中会变成锚点元素的兄弟。 Root elements of views attached to this container become siblings of the anchor element in the rendered view. 可以在元素上放置注入了 Access the |
abstract injector: Injector | 只读 该视图容器的依赖注入器。 The dependency injector for this view container. |
abstract parentInjector: Injector | 只读 |
abstract length: number | 只读 报告目前附加到本容器的视图的数量。 Reports how many views are currently attached to this container. |
方法
销毁本容器中的所有视图。 Destroys all views in this container. |
参数没有参数。 返回值
|
从该容器中获取一个视图 Retrieves a view from this container. |
实例化一个内嵌视图,并把它插入到该容器中。 Instantiates an embedded view and inserts it into this container. | |||||||||
参数
返回值新创建的这个视图的
|
实例化一个 Instantiates a single component and inserts its host view into this container. | |||||||||||||||
参数
返回值新组件的实例,包含宿主视图。
|
把一个视图插入到当前容器中。 Inserts a view into this container. | ||||||
参数
返回值插入后的 |
把一个视图移到容器中的新位置。 Moves a view to a new location in this container. | ||||||
参数
返回值移动后的 |
返回某个视图在当前容器中的索引。 Returns the index of a view within the current container. | |||
参数
返回值本视图在其容器中的从 0 开始的索引,如果没找到,则返回
|
销毁附着在该容器中的某个视图 Destroys a view attached to this container |
从当前容器中分离某个视图,但不会销毁它。 通常会和 Detaches a view from this container without destroying it. Use along with |