angular - Difference between ViewProviders and Services in Angular2? -


consider code:

var searchresultcomponent = ng.core.component({     selector: "search-result",     directives: [ng.router.router_directives, formcomponent],     viewproviders: [ng.http.http_providers],     templateurl: "componenttemplates/searchresult.html" }).class({     constructor: [ng.http.http, function( http) {         this.http = http;     }],     ngoninit: function(){     } }) 

here provided viewproviders ng.http.http_providers , service injected ng.http.http. what's difference between them. why need viewproviders.

providers used angular's dependency injection (di) resolve dependencies.

providers , services not "either or", "as as"

if component or service has dependency (like ng.http.http, in example), di looks providers tree , returns instance first provider finds.

if providers or viewproviders of component doesn't have provider requested type or token registered, di checks parent components providers providers provided bootstrap(...).

what instance component or service gets passed constructor depends on provider dependency registered.

if component b and parent component have provider specific service registered, b gets instance b. if c, sibling component of b, has dependency on service doesn't have provider registered itself, gets service instance of a.

this way can define scope of service instances.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -