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

How can I conditionally lazy-load a module (without route change)? #13984

Closed
dietergeerts opened this issue Jan 18, 2017 · 5 comments
Closed

Comments

@dietergeerts
Copy link

I'm submitting a feature request

behavior

I need a way to conditionally lazy-load a module, without a route change.

For example, on a page, the user does an action, then I want to lazy-load a module, which then will insert extra stuff in the main application. This module will be it's own file.

Another example is when the user has been logged in, based on his role, we want to add some extra stuff into our main application services etc....

(I could not find a way to do that now)

What is the motivation / use case for changing the behavior?

If this feature is possible, we won't get ugly conditional code for executing some functions, and we will not need to load the scripts that we don't need. It would be very convenient to do it.

  • Angular version: 2.3.X
@mlc-mlapis
Copy link
Contributor

mlc-mlapis commented Jan 18, 2017

You can load a module by using your own code also. It depends on which loader you are using. For SystemJS you can look at SystemJsNgModuleLoader class.

@shlomiassaf
Copy link
Contributor

shlomiassaf commented Jan 19, 2017

@dietergeerts if it's webpack just use require.ensure or import()

Once you have the module use get the NgModuleFactory to create() the module (you need to supply an injector as well)

You can look at how the router does that

@Toxicable
Copy link

This isn't a feature request since this functionality already exists on Angular's side. The rest is down to your build tools.

@tbosch
Copy link
Contributor

tbosch commented Apr 10, 2017

Just a bit more details:

  1. You need to get hold of an NgModuleFactory (e.g. via loading a *.ngfactory.js file)
  2. Call NgModuleFactory.create() -> NgModuleRef
  3. Get ComponentFactory via NgModuleRef.componentFactoryResolver
  4. Create the component

@tbosch tbosch closed this as completed Apr 10, 2017
@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 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants