Is it possible to 'pass providers' from view components to content components?
I am facing this issue over and over and cannot find neither the solution, nor the information (docs) that is not possible by design.
If I have a component in template, it can be injected in constructor of child component (this is of course working):
@Component({
selector: 'aaa',
template: '<div>This is AAA {{prop}}><div><ng-content></ng-content></div>'
})
export class AComponent {
prop = Math.random();
}
@Component({
selector: 'bbb',
template: '<div>This is BBB. Parent is AAA {{a.prop}}</div>'
})
export class BComponent {
constructor(public a: AComponent) { }
}
<aaa><bbb></bbb></aaa>
Result:
This is AAA 0.4997284193879621>
This is BBB. Parent is AAA 0.4997284193879621
But is it possible to achieve this if the component that I want to inject, is part of other component's view?
This scenario does not work:
@Component({
selector: 'ccc',
template: '<aaa><ng-content></ng-content></aaa>'
})
export class CComponent {
}
<ccc><bbb></bbb></ccc>
Ends up with NullInjectorError: R3InjectorError(AppModule)[AComponent -> AComponent -> AComponent]: NullInjectorError: No provider for AComponent!
Is there a way to write such a provider (or viewProvider) in CComponent, that would provide injectable objects from it's template/view?
Is it possible to 'pass providers' from view components to content components?
I am facing this issue over and over and cannot find neither the solution, nor the information (docs) that is not possible by design.
If I have a component in template, it can be injected in constructor of child component (this is of course working):
Result:
But is it possible to achieve this if the component that I want to inject, is part of other component's view?
This scenario does not work:
Ends up with
NullInjectorError: R3InjectorError(AppModule)[AComponent -> AComponent -> AComponent]: NullInjectorError: No provider for AComponent!Is there a way to write such a provider (or viewProvider) in CComponent, that would provide injectable objects from it's template/view?