Providers for content · Issue #42649 · angular/angular · GitHub
Skip to content

Providers for content #42649

Description

@adros

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentaction: discussarea: coreIssues related to the framework runtimecore: di

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions