What are viewproviders in Angular? And what is the difference b/w providers vs viewproviders?

In your example, there is no difference between providers and viewProviders because HelloWorld’s template doesn’t use <ng-content>. If you were projecting content within <ng-content>...</ng-content>, then Greeter couldn’t be injected in the projected content because you’re using

viewProviders: [Greeter] 

If you wanted Greeter to potentially be injected into the projected content, you’d use

providers: [Greeter]

So viewProviders limits the provider to children other than projected content, while providers allows all children to use the provider. The value is that viewProviders allows you to prevent projected content from messing with your services, which could be especially useful in libraries.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)