I'm attempting to inject ViewContainerRef
into a service, but am receiving error No provider for ViewContainerRef!
. I found this PR, which addresses exactly what I'm after and it appears to have been merged.
Note that I am aware of how to achieve this by using a placeholder using this.
ViewContainerReflink. Represents a container where one or more views can be attached to a component.
ViewContainerRef represents container where one or more view can be attached. This can contain two types of views. Host Views are created by instantiating a component using createComponent and Embedded Views are created by instantiating an Embedded Template using createEmbeddedView.
A TemplateRef represents an <ng-template /> . <ng-template let-name let-date="dateNow">{{date}} - Hello {{name}}!</ng-template> An <ng-template /> itself will not do anything at all. We need a ViewContainerRef to insert the template somewhere.
Injectors are data structures that store instructions detailing where and how services form. They act as intermediaries within the Angular DI system. Module, directive, and component classes contain metadata specific to injectors. A new injector instance accompanies every one of these classes.
Services are meant to be completely agnostic to any view. If you are trying to have a service associated with a particular view, then you should add it to the providers list within a specific parent component/directive and pass the ViewContainerRef as an argument into one of the service's methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With