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
There is no way to access inputs or outputs of Components created by NgComponentOutlet #15360
Comments
We can add events like |
Then there's no way to get the |
@matheo why do you need a |
Following the Dynamic Component Loader documentation, I would like to use the
|
@matheo |
Sweet!! thanks a ton! :) |
When would this feature be ready for release, could really use something like this :) |
If you need this feature asap you can copy paste the directive implementation until it will be merged. |
a temporary solution is to pass the data via a simple rxjs observable |
Hi! I think we really need a way to control "inputs" and "outputs". Otherwise, if we grab the reference to the component, it will just get hacky:
|
But it is the same situation as in the case of dynamically created component instances, right? |
/ref #12121 |
So someone found a way to have
This is inside a |
@DzmitryShylovich hello. Do you have any news when PR will be merged ? |
Being able to select a component to instantiate based on a runtime decision is an obvious requirement for any user interface framework. Not being able to bind inputs and outputs into that component makes the feature pretty much useless at the moment. |
FYI I exposed @sharpangles/angular-dynamic since some others were asking me for it. It exposes componentRef, separates concerns of projecting state vs dynamic loading, pre-compiles the state mapping per component type, supports child components that have no knowledge of the library, doesn't require a type reference (lazy loading), etc... There's also some stuff to help map dynamic form hierarchies I use the npm packages directly in a large enterprise system, so it gets maintained. I didn't add Output yet, but would be pretty simple to add to this implementation (if there is demand for it). |
Should be in doc |
@titusfx this hasn't been merged yet |
How exactly could I do that ? I mean, copy the code of |
You would really be better off copying and pasting the code into your own directive separate from the angular source. Call it something different and declare it in your local project. Once it has been merged , you can go change it to use the directive supplied by angular. |
We ended up using this library https://github.com/gund/ng-dynamic-component, and it worked very well for our use case. |
@waterplea |
@destus90 Thank you, but it's not what I need. I've made my own similar tool: |
@waterplea |
Well, the goal is minimal interference from my side. Of course there are tons of workarounds. Before creating the library I linked above we've had it in the form of structural directive and did all this imperative work ourselves, but that means we are responsible for the correctness of this whole code block. Passing it on to Angular to do all the magic means not only a smaller bundle but also less responsibility for us. |
For those who end up here while trying to find an easier way to use dynamic components, I found this library which provides the @Input/@output functionality currently missing from Angular core: https://www.npmjs.com/package/@ngxd/core To Google staff, Angular would be well served by directly incorporating this functionality as well as providing the ability to access these dynamic nodes with @ViewChild and @ViewChildren. |
- For loading components with inputs/outputs directives, has been necessary to use the 'ng-dynamic-component' library because of unsupported on the official Angular issue angular/angular#15360 - Allowed the dynamic components load - Grouped panels into panel component
I'm settling for a custom directive that seems to be working for me. Hopefully, it'll help someone else too
Usage:
Borrowed the idea from https://stackoverflow.com/questions/42598169/add-a-component-dynamically-to-a-child-element-using-a-directive Still hoping the idea will make its way to the Angular's src |
The ability to interact with inputs / outputs of the dynamically created component is obviously lacking in the current implementation of the
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 An alternative approach would be to deprecate the <ng-component [this]="ComponentType" input1={{exp}} input2="static" (event)="doSth()">
<div>
projectable nodes
</div>
</ng-component> Advantages:
Introducing such mechanism would require a bit more work but would offer better ergonomics and a more complete functionality. |
@pkozlowski-opensource Is there a way to make |
@devoto13 having type-safety would require:
It seems to me like we are looking at a tread-off here (dynamic / flexible vs. type safe / statically analyzable). |
@pkozlowski-opensource the |
I really like the idea 🙂. |
I've been discussing ideas around inputs / outputs of dynamically created components with @JoostK and we see 2 main options: Option 1: extend the existing
|
Option 2 would dynamically change tag name in DOM? |
@pkozlowski-opensource] Option 2 looks really like a step for the future, and both cons are logical. Btw, what about the relation to the planned feature of Support adding directives to host elements? Are there inner reasons why Option 2 should be preferred? |
@waterplea Yes and no :-) The
I don't think those proposals are related, at least I can't immediately see any relation. |
If I ask for this, because most of the time, when using |
The option with This is what ng-dynamic-component solves. At our company, we define replaceable components in the following structure: {
component: ComponentType
inputs: {
myInput: myVar
},
outputs: {
onOutput: () => onMyOutput()
}
} This is entirely configurable in Typescript, you don't need and shouldn't need a template for this. We can bind extra inputs or outputs by using the respective properties, or replace the component with a custom compatible one. The advantage of this is that any current functionality, that uses certain inputs and outputs, keeps working on the custom component. You don't need to know how the original was bound, because it will just keep working the same way after replacing the component. This is what is solved in that library and why it is very powerful. I reckon support for this should be part of the solution of this issue. |
As of Version 16.2.0 we have the ability to set the Inputs like this : |
One stop gap solution is put a rxjs Subject into your inputs and then push a value onto this subject.
Then in the component.
Make sure to unsubscribe from your subject. |
@pkozlowski-opensource
Indeed. However, in most cases if we are using dynamic components, it's to insert component that have the same interface (ie: input / output). One of our use-case is to insert filters. => All filters takes the same inputs (data types, etc..), the specificities (if there are some) are taken care in the input type interface. To address some corner cases, we could pass output listener to non-existing outputs (it will just do nothing). |
I'm submitting a ... (check one with "x")
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 ofNgComponentOutlet
. That function returns aComponentRef<any>
which provides an instance property that allows direct access to the class instance of the Component. The original feature request forNgComponentOutlet
defined an event on the directive that would fire and pass thisComponentRef
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
.The text was updated successfully, but these errors were encountered: