Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anguler2 - Unhandled Promise rejection: No provider for ViewContainerRef! ( In dynamic template )

Tags:

angular

I am trying to implement dynamic component. My requirement is, I have three class- LayoutComponent, MenuService and DynamicService. LayoutComponent is used to call the MenuService's method to perform some basic operation, after performing operation, MenuService's method again call to DynamicService's method for creating dynamic component.

Here is my Plunker with the following error which it gives

Unhandled Promise rejection: No provider for ViewContainerRef! ;

like image 871
Ranjan Avatar asked Dec 22 '16 07:12

Ranjan


1 Answers

ViewContainerRef can only be injected to components or directives, but not to services.

Components and directives get the ViewContainerRef of the element where they itself are attached. A service isn't attached to any view.

What you can do is to inject ViewContainerRef and a service to a component and then in the constructor pass the ViewContainerRef to the service. Every service or component that injects this service can access the ViewContainerRef it holds.

like image 52
Günter Zöchbauer Avatar answered Oct 04 '22 00:10

Günter Zöchbauer