Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: providing a way to get the component attached to a CdkPortalOutlet #9304

Closed
noemi-salaun opened this issue Jan 9, 2018 · 3 comments · Fixed by #9326
Closed

feat: providing a way to get the component attached to a CdkPortalOutlet #9304

noemi-salaun opened this issue Jan 9, 2018 · 3 comments · Fixed by #9326
Assignees
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix P4 A relatively minor issue that is not relevant to core functions

Comments

@noemi-salaun
Copy link

Bug, feature request, or proposal:

Feature request

When attaching a ComponentPortal to a CdkPortalOutlet using the declarative syntaxe <ng-template [cdkPortalOutlet]="myComponentPortal">, there is no way to get the instance of the component, created by the directive.

The only way to get the component instance is to attach the portal programmaticaly, but it would seem that the cycle is not as clean as using the directive directly. When doing it in the ngOnInit, the view isn't initialized, and inside the ngAfterViewInit, I get the "change after it was checked" error.

What is the use-case or motivation for changing an existing behavior?

Dynamically render a component, based on some type attribute. The developer can register it's own component to be rendered if the type matches. All these components implement the same interface and I need my parent component to access some methods in it. To do so, I need to get the instance created during the portal attachment.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular: 5.1.3
Cdk: 5.0.3
OS: Windows 10
TS: 2.4.2
Browser: Chrome 63

@noemi-salaun noemi-salaun changed the title feat: providing a way to get the component attached to a CdkPortalOutlet feat: providing a way to get the component attached to a CdkPortalOutlet Jan 9, 2018
@willshowell
Copy link
Contributor

Do you have a reproduction? Just looking at the source, it seems the portal property would be what you want.

https://github.com/angular/material2/blob/b488b393c6ea2db75322cf76a588888bc0f2a4c4/src/cdk/portal/portal-directives.ts#L71-L74

@jelbourn jelbourn added feature This issue represents a new feature or feature request rather than a bug or bug fix P4 A relatively minor issue that is not relevant to core functions labels Jan 9, 2018
@noemi-salaun
Copy link
Author

noemi-salaun commented Jan 9, 2018

The ComponentPortal returned by get portal() has access to the component type only.

https://github.com/angular/material2/blob/303e004527a74cb9da4bb894828c251741f63fbd/src/cdk/portal/portal.ts#L82-L84

The ComponentRef is created by the PortalOutlet during the attachment. But the ref isn't stored in the PortalOutlet, it is returned straight from the attach() function.

https://github.com/angular/material2/blob/303e004527a74cb9da4bb894828c251741f63fbd/src/cdk/portal/portal.ts#L184

The problem is, when using the CdkPortalOutlet directive, it's the directive that take care of the attachment, so we don't have access to the ComponentRef.

I'll add a plunker tomorrow if still necessary.

@crisbeto crisbeto self-assigned this Jan 10, 2018
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 10, 2018
Exposes the attach `ComponentRef` or `EmbeddedViewRef` in the `CdkPortalOutlet` directive.

Fixes angular#9304.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 11, 2018
Exposes the attach `ComponentRef` or `EmbeddedViewRef` in the `CdkPortalOutlet` directive.

Fixes angular#9304.
jelbourn pushed a commit that referenced this issue Jan 21, 2018
Exposes the attach `ComponentRef` or `EmbeddedViewRef` in the `CdkPortalOutlet` directive.

Fixes #9304.
jelbourn pushed a commit that referenced this issue Jan 23, 2018
Exposes the attach `ComponentRef` or `EmbeddedViewRef` in the `CdkPortalOutlet` directive.

Fixes #9304.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants