dynamic-component-loader | dynamically load components in Angular v9 | Reactive Programming library
kandi X-RAY | dynamic-component-loader Summary
kandi X-RAY | dynamic-component-loader Summary
Example of how to dynamically load components in Angular v9
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of dynamic-component-loader
dynamic-component-loader Key Features
dynamic-component-loader Examples and Code Snippets
Community Discussions
Trending Discussions on dynamic-component-loader
QUESTION
As mentioned below by Aaron, the "Other Option" approach works without issues and feels a lot cleaner. I'll leave the updates and everything in case anyone in the future has a similar problem and the workarounds provide useful somehow.
I'm having a weird problem trying to render dynamic components inside an Angular material table with pagination and sort.
Here is a Stackblitz with the reproduced issue. From what I've researched, it seems to be some sort of change detection issue, although I could be wrong on that since I have no idea why it's happening. If anyone has any ideas on how to fix this issue, or if I'm doing something blatantly wrong, feedback and help would be highly appreciated.
Current Behavior:I'm using @ViewChildren and ComponentFactoryResolver inside a material design table to render components dynamically with a few pages of data. Everything looks fine, however when using matSort to sort the data, some of the rows that have dynamic components show the wrong data values, while the rest of the row (non-dynamic) has the correct ones. See screenshots below:
Before sorting:
After sorting:
console log of component instances (note the correct status field is passed, yet the wrong one is rendered).
Expected Behavior:Data and rendered components are shown accurately when I sort. Should match the dataSource.data field.
Minimal Reproduction of the Problem with Instructions:Visit the Stackblitz and run the application. Click the sort header for "Order Status". Notice how the first entry in the table says "Delivered" while the actual dataSource has it as "Cancelled. Also not that the rest of the row is accurate - just the dynamically rendered component is incorrect.
What I've Tried:Tried setting the status with a get() and set() instead of an @Input() and register an ngOnChage, where I call a changeDetectorRef from the component itself, but the issue persisted.
Tried changing the ChangeDetectionStrategy - neither one did anything.
Tried the solutions in the below section as well
This seems to be the same issue, however it was never answered. I'm hoping providing a Stackblitz will be helpful in getting the same answer. It seems to be some sort of change detection issue according to the poster.
When loading same component with different data, old data still showing - Angular 5
Tried explicitly calling the detectChanges() and markForCheck() chanceDetectionRef methods for the componentRefs, the parent component, the actual component that gets dynamically created with no luck.
Angular dynamic component loader change detection issue
Change detection not working when creating a component via ComponentFactoryResolver
Possible Workaround / More Weird Behavior:If you sort by Order Status and get the wrong value to be in the first row as before, and then paginate to the next page and back, component shows the correct value. I'm assuming the pagination forces some sort of change detection in the material table, or the templates, or something along those lines, but I have no idea why the sort wouldn't do that. The current work around I have going is to just paginate to the next page and back, however that is by no means an acceptable solution, and I'd really like to understand what I'm doing wrong here, or what a fix for this is.
Update 1As per Aaron's additional insight below, I've added an Updated Stackblitz with a far more simplified example. On a closer look through, pagination doesn't seem to play a factor into the weird behavior. I can reproduce the issue with just 2 table entries and a sort (all pagination code removed). I've changed up the data coming in to match across the rows so it's clearer which rows come from where.
Here's a combined screenshot of before and after sorting with 2, 3, and 4, records:
I'm currently trying to see if I can force the table to re-render (assuming it's possibly shifting things around), and eventually going to dig through the source code for the table.
Update 2One workaround I was able to figure out is to set my dataSource to an empty array after I've saved a copy of it with the splice()
in the onSortChange()
right before doing my filtering logic, then setting it back to the filtered array. I'm assuming that triggers some sort of update in the material datasource/table which forces a re-render or update. I'll keep trying to figure out if there's a better approach to this, since it seems weird to be doing that, and in my original application I have a filterPredicate which also causes the same issue when filtering/resetting the filter.
Aaaaaand finally after some more research on this, I've come to at least somewhat of a solution that makes sense. After what I said on Update #2, I found this post:
https://github.com/angular/components/issues/15972#issuecomment-490235603
which explains why the table wasn't getting the updated dataSource. In my case it was probably a lot harder to figure that out since I was doing a sort/filter, and didn't think it would be directly tied to that. I'll still explore a few more ideas in the next few days, but if neither work, I'll go ahead and mark this as solved after that.
...ANSWER
Answered 2021-Mar-18 at 01:51I'm not 100% what is going on, but playing around with it a bit, I don't think the issue is change detection. I started by commenting out everything after the
cellTemplateRef.clear();
in createDynamicComponents()
in the dynamic-table.components.ts
Doing this, definitely would see everything in that column disappear.
Then taking the other approach of only commenting out the
cellTemplateRef.clear()
, you'll see your new components get shoved in on top of the old one.
Next step was the remove the old component after you added the new one...
After the Object.keys.... loop:
QUESTION
Arrgh, Having problems with Dynamically built components using ReactiveForms.
Need to trigger and Mat-Dialog and load set of components in it - works fine. Need to build some of the components dynamically from a mapping array - works fine. Need to build nested reactive forms using CVA (control value assessor) - this is the problem.
I have a working nested form using CVA that updates the form data correctly ... Unless I build the components dynamically. If I use dynamic components they are ignored in the form updates. Not sure how to fix this. My app will be very complex network of forms, so need to figure this out!
I have used the Angular.io guide for dynamic components.
Angular.io - resolve dynamic components
and followed this guide for CVA implementation
Does anyone have any Ideas?? Had issues with Stackblitz so pushed code to gitHub
...ANSWER
Answered 2020-Nov-15 at 11:43Instead of using CVA use Sub -Form Component Approach to create app-content form in this way we can register our child form with parent FormGroup using ControlContainer.
Injecting ControlContainer in child component provide access to parent FormGroup then we can add desired form control using addControl method.
content.component.ts
QUESTION
I'm implementing a relatively complex component loader in Angular, and I'd like to dynamically get the component instance from a rxjs store.
...ANSWER
Answered 2020-Aug-12 at 10:45The signature of resolveComponentFactory method looks like:
QUESTION
Given a directive used for Dynamic Component Loading with ViewContainerRef injected:
...ANSWER
Answered 2020-May-28 at 01:12ViewContainerRef
is an abstract class that is imported from @angular/core
. Because it is an abstract class, it cannot be directly instantiated. However, in your test class, you can simply create a new class which extends
the ViewContainerRef, and implements all of the required methods. Then, you can simply instantiate a new instance of the TestViewContainerRef and pass it into your FooDirective constructor in your test/spec. As such:
QUESTION
I studied the code available on angular.io, and I replicated it in my scenario with a mat-tab-group
. However, I would like to be able to use the ad-host directive inside a *ngFor
. I tried it, and the directive is always undefined
.
I searched here and, while there are several questions addressing this problem, I found no clear example on how to do this. The idea is that I have a mat-tab-group
composed of several mat-tabs
, which are loaded through a *ngFor
directive. In each tab I would like to show a different component according to the selected index. Is there any way to achieve this?
Here is my modified stackblitz: as you can see it literally says this.adHost is undefined
in the console.
ANSWER
Answered 2020-May-13 at 09:00You need to change the way you get a reference to your adHost
.
QUESTION
The current official docs only shows how to dynamically change components within an tag. https://angular.io/guide/dynamic-component-loader
What I want to achieve is, let's say I have 3 components: header
, section
, and footer
with the following selectors:
ANSWER
Answered 2017-Jul-06 at 09:28What you're trying to achieve can be done by creating components dynamically using the ComponentFactoryResolver
and then injecting them into a ViewContainerRef
. One way to do this dynamically is by passing the class of the component as an argument of your function that will create and inject the component.
See example below:
QUESTION
Can someone help me, how to use ng-template inside innerHTML tag,
Stackblitz Demo: https://stackblitz.com/edit/angular-xgnnj4
What I wanted to do is,
In the Component HTML
...ANSWER
Answered 2020-Jan-20 at 14:51You can use DomSanitizer to pass any HTML to template:
QUESTION
I'm trying to build a list of cards which may contain different components; So for example I have the following array of objects:
...ANSWER
Answered 2019-Aug-23 at 04:04You can view my blog here
First I will need to create a directive to reference to our template instance in view
QUESTION
I'm creating a generic Gantt visualization component.
The part where I will display the weeks and tasks is generic, but every row will have a header that I want to be dynamic, so for example, when displaying a list of tasks per user I want to put some user data and some action buttons, but when displaying tasks by project I want to display project data and project action buttons.
I implemented a solution following this: https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html
Everything seems to work fine, I see the actions buttons load differently in each scenario, but not the user or project data. Also, console.log(this.data.name) on the button click, I correctly see the data in the console, but if I try to print in on the template {{ data.name }}
I see nothing.
I see the following error in console:
Error: Expression has changed after it was checked. Previous value: 'CD_INIT_VALUE'. Current value: 'test project - Task 3 '. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook?
I double checked all steps, and I'm doing the exact same thing you can see on that tutorial.
Does anyone know if that tutorial is out dated?
thank you in advance for your help!
EDIT: If I change the ngAfterViewInit to ngOnInit it all works... but according to the tutorial I should be using the first. Can anyone explain to me this? :-)
...ANSWER
Answered 2019-Nov-22 at 14:18The error says it all:
It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook?
You've created component dynamically ("outside" of angular lifecycle), this means you need to control change detection manually.
So that is what you need to do:
1) After creating a component fire detectChanges() for it
2) Before disposing detach() change detection;
Here is example of my directive that creates component dynamically:
QUESTION
I am developing an Ionic + Angular 5 project.
I need to dynamically load some components. Following the Dynamic Component Loader Example in the Angular documentation I am able to successfully load components and displayed them.
I have a need, however, to be able to loop through the loaded components.
I can easily maintain my own list of components in the parent as I create and destroy the child components but it seems to me that that's ugly and not the "Angular Way".
After much experimentation and research it seems that I cannot use @ViewChildren to access dynamically added components since they have their own views. ViewChildren not finding Dynamically components
Is this correct?
I am able to use viewContainerRef.get() to iterate over the child views and retrieve ViewRefs. But there doesn't seem to be a way to get a reference to the component given a ViewRef?
I notice the 'rootNodes' property of the ViewRef however that property is not documented in the Angular docs View Ref However, it is documented in the EmbeddedViewRef docs Embedded View Ref but that still doesn't seem to give me access to the component.
So the question is when I have a parent that dynamically adds child components how can I from the parent loop through the child components (so I can call one of my methods in the child)? My suspicion is that I am missing something stupid simple, have some fundamental misconception, or that there is a different pattern that I should be using.
I've modified the Dynamic Component Loader example to highlight what I am trying to do.Modified Dynamic Component Loader
...ANSWER
Answered 2019-Nov-08 at 21:45So, I thought about this and a clean way to do this (in my opinion), is to pass create a property on the parent component, such as parentContext
and to pass the data or method into this object.
Example - in parent component, you can even pass a method into your child component and it will run within the context of the parent.:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install dynamic-component-loader
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page