Lecturer: This video is going to cover using ocLazyLoad for lazy loading in AngularJS. If you want to jump ahead, you can jump straight into his examples, and look at how he uses it and those scenarios.
We're going to stick with the basics in this video, and then, grow up to those examples. If you need to install it, just go ahead and do bower install ocLAzyLoad, or grab it some other way. I have a super basic application here where if I click this button it's going to call app.click, which is going to say, "Console log clicked." I'll refresh here.
I'll show the console, click the button, and it shows, "Clicked."
Now, for lazy loading, I'm going to load this store module later on, after the bootstrapping process. For example, if I load the store module into my demo module right now, and I use the injector I can say "$injector.get" and I can get a service or anything off of my module that's loaded.
$ injector.get "cart" , when I refresh and I click you can see that I get that service back. It has that buy function on it and everything ready to go. If I don't load this module here, if I don't load the store module and I refresh you'll see that when I try to use the injector to get the cart that it's an unknown provider. That cart is not there to use it.
Lazy loading is going to solve that problem. I'm going to use the oc.lazyLoad module, capital L right here. I already included this script here. When I click I'm going to say, "I'm going to use the oc.LazyLoad service so that when I click I'll say, "$ocLazyLoad.load," and then, I can say the name of the module that I want to load, "Name" and I want to load the store module. I'm loading in this guy.
It's not defined up here. It's just defined down here. Then, this does return a promise. When it's done, on success I'm going to use the injector to get the cart again. I'll just move this guy up and then save this, refresh, and click.
This time I can get that cart service one more time, because the injector can find the cart because ocLazyLoad loaded the store module after my app had already started up.
Again, if I were to try this outside of this click, meaning out of the ocLazyLoad loading that in and refresh, it shows that that provider is unknown. It's not ready. But if I move it back into after ocLazyLoad has had a chance to load that store and I refresh you can see that it's there for me.
What makes this much more powerful is if I create another script here. I'll just call this store. I'll move the store outside of my demo.js. I'll save it here. Just going to leave that code alone.
You'll see that I have my demo. I'm going to try and load it here. It's going to fail, because when I say, "Click me," it didn't successfully load. I'd actually have to put another function in here and say, "console.log failed to load module " refresh, and say, "Click me." You see it failed to load that module. It couldn't find store.
I'm just going to get rid of this error handler. I'm not going to use it again. And then, instead of just passing in a name I can also pass in a set of files that are associated with that module.
If I check my network, I'll clear out my entire network, refresh, we'll clear out again. You'll see I hit, "Click me," and it actually loads in a store.js after I've clicked. This was just a quick intro. This isn't the typical use case. Usually, you would put this in a routing solution where you navigate to a new page and on that new page you'd want to load in new modules and new features.
It's really a call on your end you have to make as to whether you need to lazy load or not. Typically I'd say you don't need to and you don't have to but if you're working on a large project and it's a huge load up front then lazy loading is something you should look into.