Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way of lazy loading data for an ng-include in Angular?

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?

like image 400
Martijn Avatar asked Jun 06 '13 13:06

Martijn


People also ask

What is the lazy loading in Angular?

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.

How is lazy loading implemented in Angular components?

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.

Why lazy loading is important in Angular?

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.

How do you lazy load a component?

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.


1 Answers

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:

  1. url is initially not set to anything so ng-include has nothing to fetch
  2. When clicking on a parent item set the url variable to something using ng-click (you can use a scope function for more control over the url like in this example)
  3. The variable inside that scope is now set so ng-include can fetch the url as it is requested on click

Note: 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.

like image 132
Gloopy Avatar answered Nov 15 '22 04:11

Gloopy