I create (parent) rows when looping through a list of objects using ng-repeat
. I also create a hidden row. The visible row contains soms basic data and the hidden row (detail) contains all the data. I want the detail row to be visible when the parent row is clicked. And I only want to load the data when the details row becomes visible. Also, once the details of a row are already loaded, I don't want to go to server again.
I've created a partial for the details view, so my code looks like this:
<div ng-include="'/Customer/View'"></div>
Off course I can locate to /Customer/View/12
, but then all the data is loaded, even when there's no request.
So, what is the way to go to accomplish this? Can someone point me in the right direction?
Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. It improves the speed of the application load time by splitting the application into several bundles. When the user navigates through the app, the bundles are loaded as required.
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy const routes: Routes = [ { path: 'items', loadChildren: () => import('./items/items. module'). then(m => m.
Lazy loading is an important Angular feature that helps to reduce the initial load time since it loads only the necessary files first. Other required modules are loaded on demand when you navigate to their particular route. Now, you can take advantage of this feature to improve your app's load time.
Lazy loading is the technique used in optimizing your web and mobile apps, this works by rendering only needed or critical user interface items first, then quietly rendering the non-critical items later. As we build code components the application grows, and the bundle gets very cumbersome in size.
One way you can accomplish this is to use a variable with your ngInclude and set it on click of the parent row.
Here is a sample fiddle and if you use the network tab of Chrome (or other browser) you can see that the url in the include won't be requested until you click on an item.
<div ng-repeat="item in items" ng-click="url = item">
<span ng-hide="url">Click to load</span>
<span ng-show="url">Loaded item {{url}}</span>
<div ng-include="url"></div>
</div>
At a high level what's going on here is:
url
is initially not set to anything so ng-include
has nothing to fetchurl
variable to something using ng-click
(you can use a scope function for more control over the url like in this example)ng-include
can fetch the url as it is requested on clickNote: You won't have to declare the url
variable in javascript since ngRepeat creates a new scope for each item you are looping through so the click action setting the variable will only affect the current scope you are working with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With