question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is no way to access inputs or outputs of Components created by NgComponentOutlet

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior There is no way to set @Inputs or subscribe to @Output events of the Component created by NgComponentOutlet.

Expected behavior I expect some to set values and to subscribe events.

What is the motivation / use case for changing the behavior? At least, we should have the same level of functionality as ViewContainerRef.createComponent() which is roughly the programatic equivalent of NgComponentOutlet. That function returns a ComponentRef<any> which provides an instance property that allows direct access to the class instance of the Component. The original feature request for NgComponentOutlet defined an event on the directive that would fire and pass this ComponentRef object after creation. This did not make it into the current implementation and I see no other alternatives to access inputs/outputs.

Supporting only Components with no inputs or outputs severely limits the usefulness of NgComponentOutlet.

  • Angular version: 4.0.0-rc.1

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:118
  • Comments:61 (30 by maintainers)

github_iconTop GitHub Comments

38reactions
ybrodskycommented, Oct 6, 2017

@meriturva struggling with this shit for 1.5 days now. Solved the same problem in React in 15 minutes, 1 google search and common sense.

33reactions
pkozlowski-opensourcecommented, Jul 6, 2022

The ability to interact with inputs / outputs of the dynamically created component is obviously lacking in the current implementation of the ngComponentOutlet and there are reasons why we didn’t add it so far:

So at this point we can consider the inputs / outputs issues to be “solved” (even if more work is needed). This leaves us with the API design question.

As I’ve mentioned we are not excited about the proposed <ng-container *ngComponentOutlet="component; inputs:{name: 'foo'}; outputs:{select: onSelect}"> API. The main problem is that it represents inputs and outputs as keys on an object literal. This is not what we do in other framework APIs and it pretty much bypasses existing change detection mechanisms. The other problem is that we don’t support content projection.

An alternative approach would be to deprecate the ngComponentOutlet directive and have a dedicated tag in Angular (working name: ng-component), that could be used like:

<ng-component [this]="ComponentType" input1={{exp}} input2="static" (event)="doSth()">
    <div>
        projectable nodes
    </div>
</ng-component>

Advantages:

  • dynamically creating a component becomes very similar to creating a regular component;
  • interacting with inputs / outputs is identical to regular components;
  • content projection can be supported.

Introducing such mechanism would require a bit more work but would offer better ergonomics and a more complete functionality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass an input value into a ngComponentOutlet created ...
A workaround is to add a Service that initialize your component, and with ngComponentOutletInjector inject a custom Injector. One that you can doit...
Read more >
NgComponentOutlet - Angular
NgComponentOutlet requires a component type, if a falsy value is set the view will clear and any existing component will be destroyed.
Read more >
Angular:Passing data in and out of Dynamic Components ...
I have already written a story which explores how to pass data in and out of Dynamic Components created using ComponentFactoryResolver.
Read more >
ng-dynamic-component - GitBook
Dynamic components with full life-cycle support for inputs and outputs ... You can also use NgComponentOutlet directive from @angular/common instead of ...
Read more >
Advance Angular Dynamic Component - DEV Community ‍ ‍
In order to create a dynamic component, you have to use either ngComponentOutlet ... neither provides a way to bind inputs and outputs....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found