ApplicationRef
A reference to an Angular application running on a page.
class ApplicationRef {}
destroyed
boolean
Indicates whether this instance was destroyed.
componentTypes
Type<any>[]
Get a list of component types registered to this application. This list is populated even before the component is created.
components
ComponentRef<any>[]
Get a list of components registered to this application.
isStable
Observable<boolean>
Returns an Observable that indicates when the application is stable or unstable.
whenStable
Promise<void>
Promise<void>
injector
The EnvironmentInjector
used to create this application.
bootstrap
Bootstrap a component onto the element identified by its selector or, optionally, to a specified element.
any
ComponentRef<C>
Bootstrap process
When bootstrapping a component, Angular mounts it onto a target DOM element
and kicks off automatic change detection. The target DOM element can be
provided using the rootSelectorOrNode
argument.
If the target DOM element is not provided, Angular tries to find one on a page
using the selector
of the component that is being bootstrapped
(first matched element is used).
Example
Generally, we define the component to bootstrap in the bootstrap
array of NgModule
,
but it requires us to know the component while writing the application code.
Imagine a situation where we have to wait for an API call to decide about the component to
bootstrap. We can use the ngDoBootstrap
hook of the NgModule
and call this method to
dynamically bootstrap a component.
{@example core/ts/platform/platform.ts region='componentSelector'}
Optionally, a component can be mounted onto a DOM element that does not match the selector of the bootstrapped component.
In the following example, we are providing a CSS selector to match the target element.
{@example core/ts/platform/platform.ts region='cssSelector'}
While in this example, we are providing reference to a DOM node.
{@example core/ts/platform/platform.ts region='domNode'}
Bootstrap a component onto the element identified by its selector or, optionally, to a specified element.
any
ComponentRef<C>
Bootstrap process
When bootstrapping a component, Angular mounts it onto a target DOM element
and kicks off automatic change detection. The target DOM element can be
provided using the rootSelectorOrNode
argument.
If the target DOM element is not provided, Angular tries to find one on a page
using the selector
of the component that is being bootstrapped
(first matched element is used).
Example
Generally, we define the component to bootstrap in the bootstrap
array of NgModule
,
but it requires us to know the component while writing the application code.
Imagine a situation where we have to wait for an API call to decide about the component to
bootstrap. We can use the ngDoBootstrap
hook of the NgModule
and call this method to
dynamically bootstrap a component.
{@example core/ts/platform/platform.ts region='componentSelector'}
Optionally, a component can be mounted onto a DOM element that does not match the selector of the bootstrapped component.
In the following example, we are providing a CSS selector to match the target element.
{@example core/ts/platform/platform.ts region='cssSelector'}
While in this example, we are providing reference to a DOM node.
{@example core/ts/platform/platform.ts region='domNode'}
tick
void
Invoke this method to explicitly process change detection and its side-effects.
In development mode, tick()
also performs a second change detection cycle to ensure that no
further changes are detected. If additional changes are picked up during this second cycle,
bindings in the app have side-effects that cannot be resolved in a single change detection
pass.
In this case, Angular throws an error, since an Angular application can only have one change
detection pass during which all change detection must complete.
void
attachView
void
Attaches a view so that it will be dirty checked. The view will be automatically detached when it is destroyed. This will throw if the view is already attached to a ViewContainer.
void
detachView
void
onDestroy
VoidFunction
Registers a listener to be called when an instance is destroyed.
() => void
A callback function to add as a listener.
VoidFunction
destroy
void
Destroys an Angular application represented by this ApplicationRef
. Calling this function
will destroy the associated environment injectors as well as all the bootstrapped components
with their views.
void
viewCount
number
Returns the number of attached views.