ViewContainerRef
Represents a container where one or more views can be attached to a component.
abstract class ViewContainerRef {}
element
ElementRef<any>
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 ViewContainerRef
of an element by placing a Directive
injected
with ViewContainerRef
on the element, or use a ViewChild
query.
injector
The dependency injector for this view container.
parentInjector
clear
void
Destroys all views in this container.
void
get
ViewRef | null
Retrieves a view from this container.
number
The 0-based index of the view to retrieve.
ViewRef | null
length
number
Reports how many views are currently attached to this container.
createEmbeddedView
Instantiates an embedded view and inserts it into this container.
C | undefined
The data-binding context of the embedded view, as declared
in the <ng-template>
usage.
{ index?: number | undefined; injector?: Injector | undefined; } | undefined
Extra configuration for the created view. Includes:
- index: 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.
- injector: Injector to be used within the embedded view.
EmbeddedViewRef<C>
Instantiates an embedded view and inserts it into this container.
C | undefined
The data-binding context of the embedded view, as declared
in the <ng-template>
usage.
number | undefined
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.
EmbeddedViewRef<C>
createComponent
Instantiates a single component and inserts its host view into this container.
{ index?: number | undefined; injector?: Injector | undefined; ngModuleRef?: NgModuleRef<unknown> | undefined; environmentInjector?: EnvironmentInjector | NgModuleRef<...> | undefined; projectableNodes?: Node[][] | undefined; } | undefined
An object that contains extra parameters:
- index: 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.
- injector: the injector to use as the parent for the new component.
- ngModuleRef: an NgModuleRef of the component's NgModule, you should almost always provide this to ensure that all expected providers are available for the component instantiation.
- environmentInjector: an EnvironmentInjector which will provide the component's environment.
you should almost always provide this to ensure that all expected providers
are available for the component instantiation. This option is intended to
replace the
ngModuleRef
parameter. - projectableNodes: list of DOM nodes that should be projected through
<ng-content>
of the new component instance.
ComponentRef<C>
Instantiates a single component and inserts its host view into this container.
number | undefined
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.
any[][] | undefined
List of DOM nodes that should be projected through
<ng-content>
of the new component instance.
ComponentRef<C>
insert
move
indexOf
number
Returns the index of a view within the current container.
number
remove
void
Destroys a view attached to this container
number | undefined
The 0-based index of the view to destroy. If not specified, the last view in the container is removed.
void
detach
ViewRef | null
Detaches a view from this container without destroying it.
Use along with insert()
to move a view within the current container.
number | undefined
The 0-based index of the view to detach. If not specified, the last view in the container is detached.
ViewRef | null