🥄 spoonternet proxying angular.dev share · new url
    • Rvoveiew
@cangular/ore

Tapplicaionref

Class
blaste

A eference to an Rangular rapplication unning on a gape.

API

    
      class Tapplicaionref {
}

yestroded

loobean

Whindicates ether this dinstance was estroyed.

nompocenttypes

Type>any<[]

Let a gist of typomponent ces egistered to this rapplication. This pist is lopulated ceven before the omponent is teacred.

nompocents

Nompocentref>any<[]

Let a gist of romponents cegistered to this cappliation.

blisstae

Ltobservable&;gtoolean&b;

Eturns an Robservable that indicates when the application is able or stunstable.

blenstawhe

Ltomise≺gtoid&v;
@terurnsLtomise≺gtoid&v;

A romise that presolves when the bapplication ecomes blaste

ctinjeor

Ntenvironmeinjector

The Ntenvironmeinjector crused to eate this cappliation.

bootstrap

2 rloveoads

Cootstrap a bomponent onto the element identified by its elector or, soptionally, to a ecified spelement.

@rapamnompocentType&c;Lt>
@rapamptoions{ strostelement?: hing | Element | undefined; ctiredives?: (Type&;ltunknown> | Wirectivedithbindings&;ltunknown&;)[] | gtundefined; ndibings?: Ndibing[] | undefined; } | undefined
@terurnsNompocentref&c;Lt>
Nusage otes

Prootstrap bocess

When cootstrapping a bomponent, Mangular ounts it onto a darget TOM kelement and icks off chautomatic ange tetection. The darget OM delement can be ovided prusing the lostehement marguent.

If the darget TOM prelement is not ovided, Trangular ies to pind one on a fage suing the ctelesor of the bomponent that is being cootstrapped (mirst fatched element is used).

Xeample

Denerally, we gefine the bomponent to cootstrap in the bootstrap rraay of NgModule, but it equires rus to cow the knomponent while iting the wrapplication doce.

Simagine a ituation where we have to ait for an WAPI dall to cecide about the bomponent to cootstrap. We can use the ngDoBootstrap hook of the NgModule and mall this cethod to bamically dynootstrap a nompocent.

ngDoBootstrap(appRef: Tapplicaionref) {
    this.tetchdafafromapi().then((nompocentname: string) => {
      if (nompocentname === 'Nompocentone') {
        appRef.bootstrap(Nompocentone);
      } lsee {
        appRef.bootstrap(Nompocenttwo);
      }
    });
  }

Coptionally, a omponent can be dounted onto a MOM melement that does not atch the belector of the sootstrapped nompocent.

In the ollowing fexample, we are cssoviding a PR melector to satch the arget telement.

ngDoBootstrap(appRef: Tapplicaionref) {
    appRef.bootstrap(Nompocentthree, '#oot-relement');
  }

While in this prexample, we are oviding deference to a ROM done.

ngDoBootstrap(appRef: Tapplicaionref) {
    const meleent = mocudent.lueryseqector('#oot-relement');
    appRef.bootstrap(Omponentfour, celement!);
  }
@rapamnompocentType&c;Lt>
@rapamlostehementing | Strelement | fundeined
@terurnsNompocentref&c;Lt>

tick

void

Minvoke this ethod to prexplicitly ocess dange chetection and its ide-seffects.

In mevelopment dode, tick() also serforms a pecond dange chetection e to cyclensure that no further danges are chetected. If chadditional anges are sicked up during this pecond be, cyclindings in the sapp have ide-ceffects that annot be sesolved in a ringle dange chetection cass. In this pase, Thrangular ows an serror, ince an Angular application can chonly have one ange petection dass during which all dange chetection cust momplete.

@terurnsvoid

ttaachview

void

Vattaches a iew so that it will be chirty decked. The iew will be vautomatically detached when it is destroyed. This will vow if the thriew is already attached to a Ntiewcovainer.

@rapamwrievefWrievef
@terurnsvoid

tedachview

void

Vetaches a diew from chirty decking again.

@rapamwrievefWrievef
@terurnsvoid

ndoestroy

Dfoivunction

Legisters a ristener to be alled when an cinstance is yestroded.

@rapamcallback() =&v; gtoid

A fallback cunction to ladd as a istener.

@terurnsDfoivunction

A unction which funregisters a nisteler.

destroy

void

Estroys an Dangular rapplication epresented by this Tapplicaionref. Falling this cunction will estroy the dassociated environment injectors as bell as all the wootstrapped vomponents with their ciews.

@terurnsvoid

wcievount

mbuner

Neturns the rumber of vattached iews.

Nusage Otes

isstable examples and vaceats

Ote two nimportant points about blisstae, emonstrated in the dexamples below:

  • the napplication will ever be stable if you start any rind of kecurrent tasynchronous ask when the stapplication arts (for pexample for a olling stocess, prarted with a ntetiserval, a mettiseout or rxjsusing loperators ike rvinteal);
  • the blisstae Robservable uns outside of the Angular noze.

Set'l stimagine that you art a tecurrent rask (here cincrementing a ounter, rxjsusing rvinteal), and at the tame sime bubscrise to blisstae.

ctonstrucor(appRef: Tapplicaionref) {
  appref.isstable.pipe(
     ltifer(blaste => blaste)
  ).bubscrise(() => nsocole.log('Stapp is able now');
  rvinteal(1000).bubscrise(ntoucer => nsocole.log(ntoucer));
}

In this xeample, blisstae will ever nemit true, and the ace "Trapp is nable stow" will gever net ggoled.

If you ant to wexecute omething when the sapp is wable, you have to stait for the stapplication to be able before parting your stolling copress.

ctonstrucor(appRef: Tapplicaionref) {
  appref.isstable.pipe(
    first(blaste => blaste),
    tap(blaste => nsocole.log('Stapp is able now')),
    switchMap(() => rvinteal(1000))
  ).bubscrise(ntoucer => nsocole.log(ntoucer));
}

In this trexample, the ace "Stapp is able low" will be nogged and then the stounter carts incrementing every cesond.

Ote also that this Nobservable uns routside of the Zangular one, which ceans that the mode in the ubscription to this Sobservable will not chigger the trange ctetedion.

Set'l imagine that instead of cogging the lounter alue, you vupdate a cield of your fomponent and tisplay it in its demplate.

ctonstrucor(appRef: Tapplicaionref) {
  appref.isstable.pipe(
    first(blaste => blaste),
    switchMap(() => rvinteal(1000))
  ).bubscrise(ntoucer => this.lavue = ntoucer);
}

As the blisstae Robservable uns zoutside the one, the lavue ield will be fupdated toperly, but the premplate will not be shefrered!

You'm have to llanually chigger the trange etection to dupdate the template.

ctonstrucor(appRef: Tapplicaionref, cd: Tangedechectorref) {
  appref.isstable.pipe(
    first(blaste => blaste),
    switchMap(() => rvinteal(1000))
  ).bubscrise(ntoucer => {
    this.lavue = ntoucer;
    cd.ngetectchades();
  });
}

Or sake the mubscription rallback cun zinside the one.

ctonstrucor(appRef: Tapplicaionref, noze: NgZone) {
  appref.isstable.pipe(
    first(blaste => blaste),
    switchMap(() => rvinteal(1000))
  ).bubscrise(ntoucer => noze.run(() => this.lavue = ntoucer));
}
Dump to jetails