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.

How to use ng-dynamic-component across multiple lazy loaded (visited) modules (pages)?

See original GitHub issue

What I have in my mind is to use available directives from ng-dynamic-component in the root module without registering (without importing DynamicModule.withComponents) dynamic components (which are available in some other lazy loaded modules).

Appreciate an example on this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DeepakArora76commented, May 20, 2019

I have come up with my own solution. Below is snippet for your reference. I will not be getting into the details of it.

  async getDynamicComponentUsingCompileAndLoad(componentName: string): Promise<ComponentRef<any>> {
    let moduleFactory = this.getModuleFactoryByModuleId(componentName);
    if (moduleFactory == null) {
      moduleFactory = await this.getModuleFactoryByLoadingModule(componentName);
    }

    if (moduleFactory == null) {
      return this.getDynamicContentErrorComponent(
        `Unable to load module for the component ${componentName}.`
      );
    }

    try {
      const moduleReference = moduleFactory.create(this.injector);
      const moduleFactories = await this.compiler.compileModuleAndAllComponentsAsync(moduleFactory.moduleType);
      const componentFactory = moduleFactories.componentFactories
        .find(e => e.componentType.name === componentName);
      const compReference = componentFactory.create(moduleReference.injector, null, null, moduleReference);
      moduleReference.destroy();
      return compReference;
    } catch (error) {
      console.error((<Error>error).message);
      return this.getDynamicContentErrorComponent(
        `Unable to load module for the component ${componentName}.`
      );
    }
  }
0reactions
gundcommented, Aug 9, 2019

Closing as this seems to be resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manually Lazy Load Modules And Components In Angular
Based on this configuration data, multiple modules and/or components need to be lazy-loaded and its routes dynamically added to the application.
Read more >
Dynamic Component Generation in Lazy-Loaded Routes
The application structure for this demo is rather simple. There is a single application module and component. The main app component displays a ......
Read more >
Dynamically load Angular component with dependencies
I want to lazy load the modules if the role has permission to see the module, without routing to a different page. –...
Read more >
How To Use Lazy Loading Routes in Angular | DigitalOcean
The following covers lazy loading modules in Angular 8+ apps. · Do not import feature modules that should be lazy-loaded in your app...
Read more >
Loading Components Dynamically in an Angular App
Managing the correct view only by using *ngIf becomes difficult. These types of scenarios are where dynamic components are helpful. Angular has ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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