ViewContainerRef

export class ViewContainerRef

exported from angular2/core defined in angular2/src/core/linker/view_container_ref.ts (line 11)

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 createHostView, and Embedded Views, created by instantiating an Embedded Template via 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.

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 AppViewManager.

Members

element : ElementRef

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

clear() : void

Destroys all Views in this container.

get(index: number) : ViewRef

Returns the ViewRef for the View located in this container at the specified index.

length : number

Returns the number of Views currently attached to this container.

createEmbeddedView(templateRef: TemplateRef, index?: number) : ViewRef

Instantiates an Embedded View based on the 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 ViewRef for the newly created View.

createHostView(protoViewRef?: ProtoViewRef, index?: number, dynamicallyCreatedProviders?: ResolvedProvider[]) : HostViewRef

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

The component is instantiated using its protoView which can be obtained via Compiler.

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

You can optionally specify dynamicallyCreatedProviders, which configure the Injector that will be created for the Host View.

Returns the HostViewRef of the Host View created for the newly instantiated Component.

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

Inserts a View identified by a 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 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.

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

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

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