填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

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.

可以在元素上放置注入了 ViewContainerRefDirective 來訪問元素的 ViewContainerRef。也可以使用 ViewChild 進行查詢。

Access the ViewContainerRef of an element by placing a Directive injected with ViewContainerRef on the element, or use a ViewChild query.

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.

abstract clear(): void
      
      abstract clear(): void
    
引數

沒有引數。

返回值

void

從該容器中獲取一個檢視

Retrieves a view from this container.

abstract get(index: number): ViewRef | null
      
      abstract get(index: number): ViewRef | null
    
引數
index number

所要獲取檢視的從 0 開始的索引。

The 0-based index of the view to retrieve.

返回值

ViewRef 實例,如果索引超出範圍則為 0。

ViewRef | null: The ViewRef instance, or null if the index is out of range.

實例化一個內嵌檢視,並把它插入到該容器中。

Instantiates an embedded view and inserts it into this container.

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

用來定義檢視的 HTML 範本。

The HTML template that defines the view.

context C
可選. 預設值是 `undefined`.
index number

從 0 開始的索引,表示新檢視要插入到當前容器的哪個位置。 如果沒有指定,就把新的檢視追加到最後。

The 0-based index at which to insert the new view into this container. If not specified, appends the new view as the last entry.

可選. 預設值是 `undefined`.
返回值

新建立的這個檢視的 ViewRef 實例。

EmbeddedViewRef<C>: The ViewRef instance for the newly created view.

實例化一個 Component並把它的宿主檢視插入到本容器的指定 index 處。

Instantiates a single component and inserts its host view into this container.

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

要使用的工廠。

The factory to use.

index number

從 0 開始的索引,表示新元件的宿主檢視要插入到當前容器的哪個位置。 如果沒有指定,就把新的檢視追加到最後。

The index at which to insert the new component's host view into this container. If not specified, appends the new view as the last entry.

可選. 預設值是 `undefined`.
injector Injector

一個注入器,將用作新元件的父注入器。

The injector to use as the parent for the new component.

可選. 預設值是 `undefined`.
projectableNodes any[][]
可選. 預設值是 `undefined`.
ngModule NgModuleRef
可選. 預設值是 `undefined`.
返回值

新元件的實例,包含宿主檢視。

ComponentRef<C>: The new component instance, containing the host view.

把一個檢視插入到當前容器中。

Inserts a view into this container.

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

要插入的檢視。

The view to insert.

index number

從 0 開始的索引,表示該檢視要插入到當前容器的哪個位置。 如果沒有指定,就把新的檢視追加到最後。

The 0-based index at which to insert the view. If not specified, appends the new view as the last entry.

可選. 預設值是 `undefined`.
返回值

插入後的 ViewRef 實例。

ViewRef: The inserted ViewRef instance.

把一個檢視移到容器中的新位置。

Moves a view to a new location in this container.

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

要移動的檢視。

The view to move.

currentIndex number
返回值

移動後的 ViewRef 實例。

ViewRef: The moved ViewRef instance.

返回某個檢視在當前容器中的索引。

Returns the index of a view within the current container.

abstract indexOf(viewRef: ViewRef): number
      
      abstract indexOf(viewRef: ViewRef): number
    
引數
viewRef ViewRef

要查詢的檢視。

The view to query.

返回值

本檢視在其容器中的從 0 開始的索引,如果沒找到,則返回 -1

number: The 0-based index of the view's position in this container, or -1 if this container doesn't contain the view.

銷燬附著在該容器中的某個檢視

Destroys a view attached to this container

abstract remove(index?: number): void
      
      abstract remove(index?: number): void
    
引數
index number

要銷燬的檢視的從 0 開始的索引。 如果不指定 index,則移除容器中的最後一個檢視。

The 0-based index of the view to destroy. If not specified, the last view in the container is removed.

可選. 預設值是 `undefined`.
返回值

void

從當前容器中分離某個檢視,但不會銷燬它。 通常會和 insert() 一起使用,在當前容器中移動一個檢視。

Detaches a view from this container without destroying it. Use along with insert() to move a view within the current container.

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

要分離的檢視的從 0 開始的索引。 如果省略 index 引數,則拆出最後一個 ViewRef

The 0-based index of the view to detach. If not specified, the last view in the container is detached.

可選. 預設值是 `undefined`.
返回值

ViewRef | null