Injection in Angular 2. How does it work? -


i'm learning angular 2, , i've been learning few days. when read injectable() concept in angular 2 , try applying tuturial's example code follow link : angular 2 - dependency injection, have got problem.

they said need putting annotation @injectable() in top of class other class can inject, :

import {     injectable  } '@angular/core';   @injectable()  export class appservice {      getapp(): string {        return "hello world";     }  } 

the problem when drop injectable(), app still running well? what's wrong me? incidentally, can me figure out " how injection in angular 2 work?"

as stated in documentation:

as happens, have omitted @injectable() first version of heroservice because had no injected parameters. must have service has injected dependency. need because angular requires constructor parameter metadata in order inject logger.

https://angular.io/guide/dependency-injection#why-injectable

so in short, if injectables have injectors, @injectable decorator resolves cyclic-dependency.


" how injection in angular 2 work?"

this broad question sum angular injection system creates instance of provider object/function , uses instance in component when inject in constructor.

if haven't provided in component using go it's parent component, module it's been used until finds instance. each level has own map of provider instances , component use first instance finds when traverses injection tree upwards.

so provider singleton instance point defined.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -