class ViewContainerRef { get element(): ElementRef get injector(): Injector get parentInjector(): Injector clear(): void get(index: number): ViewRef|null get length(): number createEmbeddedView<C>(templateRef: TemplateRef<C>,context?: C,index?: number): EmbeddedViewRef<C> createComponent<C>(componentFactory: ComponentFactory<C>,index?: number,injector?: Injector,projectableNodes?: any[][],ngModule?: NgModuleRef<any>): ComponentRef<C> insert(viewRef: ViewRef,index?: number): ViewRef move(viewRef: ViewRef,currentIndex: number): ViewRef indexOf(viewRef: ViewRef): number remove(index?: number): void detach(index?: number): ViewRef|null }
描述
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 Component via createComponent,and Embedded Views,created by instantiating an Embedded Template via createEmbeddedView.
容器可以包含两种视图。
Host Views
通过使用createComponent实例化组件创建
Embedded Views
通过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.
View Container在包含视图中的位置由锚点元素指定。每个View Container只能有一个锚点元素,每个锚点元素只能有一个View Container
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 Directive injected with ViewContainerRef on the Element,or you obtain it via a ViewChild query.
要访问一个元素的ViewContainerRef,您可以在元素上注入ViewContainerRef ,也可以通过ViewChild查询获取。
方法成员
- get element(): ElementRef
Anchor element that specifies the location of this container in the containing View.
在包含视图中指定此容器的位置的锚点元素。
- get injector(): Injector
- get parentInjector(): Injector
- clear(): void
Destroys all Views in this container.
销毁此容器中的所有视图。
- get(index: number): ViewRef|null
Returns the ViewRef for the View located in this container at the specified index.
返回指定索引位于此容器中的视图的ViewRef。
- get length(): number
Returns the number of Views currently attached to this container.
返回当前附加到此容器的视图数
- createEmbeddedView(templateRef: TemplateRef,context?: C,
index?: number): EmbeddedViewRef
Instantiates an Embedded View based on the templateRef
and inserts it into this container at the specified index.
基于“templateRef”实例化嵌入式视图,并将其插入到指定索引的此容器中。
If index is not specified,the new View will be inserted as the last View in the container.
如果未指定索引,则将在容器中作为最后一个视图插入新的“视图”
Returns the ViewRef for the newly created View.
返回新创建的View的ViewRef。
- createComponent(componentFactory: ComponentFactory,index?:
number,injector?: Injector,projectableNodes?: any[][],ngModule?:
NgModuleRef): ComponentRef
Instantiates a single Component and inserts its Host View into this container at the specified index.
实例化单个组件,并将其主机视图插入到指定索引的此容器中。
The component is instantiated using its ComponentFactory which can be obtained via ComponentFactoryResolver.
该组件使用其ComponentFactory进行实例化,该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 Injector that will be used as parent for the Component.
您可以选择指定注射器用作组件的父级。
Returns the ComponentRef of the Host View created for the newly instantiated Component.
返回为新实例化的组件创建的主机视图的ComponentRef。
- insert(viewRef: ViewRef,index?: number): ViewRef
Inserts a View identified by a ViewRef into the container at the specified index.
将由ViewRef标识的视图插入到指定索引的容器中
If index is not specified,the new View will be inserted as the last View in the container.
如果未指定索引,则将在容器中作为最后一个视图插入新的“视图”。
Returns the inserted ViewRef.
返回插入的ViewRef。
- move(viewRef: ViewRef,currentIndex: number): ViewRef
Moves a View identified by a ViewRef into the container at the specified index.
将ViewRef标识的视图移动到指定索引的容器中。
Returns the inserted ViewRef.
返回插入的ViewRef
indexOf(viewRef: ViewRef): number
Returns the index of the View,specified via ViewRef,within the current container or -1 if this container doesn’t contain the View.
返回通过ViewRef在当前容器中指定的View的索引,如果此容器不包含View,则返回-1。
- remove(index?: number): void
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.
如果未指定索引,则将删除容器中的最后一个。
- detach(index?: number): ViewRef|null
Use along with insert to move a View within the current container.
与insert一起使用,用来移动当前容器内的视图
If the index param is omitted,the last ViewRef is detached.
如果省略了索引参数,则最后一个ViewRef被分离。