├── .gitignore ├── CHANGELOG.md ├── README.md ├── dist ├── lazyFor.directive.d.ts ├── lazyFor.directive.js ├── lazyFor.directive.metadata.json ├── lazyFor.module.d.ts ├── lazyFor.module.js └── lazyFor.module.metadata.json ├── package.json ├── src ├── lazyFor.directive.ts └── lazyFor.module.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## 1.1.5 - 2017-10-31 4 | - Fixed issue that would occur if the list started out being empty 5 | 6 | ## 1.1.4 - 2017-10-20 7 | - Fixed issue with clearing large list of items 8 | 9 | ## 1.1.3 - 2017-04-24 10 | - Added check to only regenerate DOM elements if items in the list have changed or a scroll event has fired. This greatly improves performance in some cases. 11 | - Added this change log 12 | 13 | ## 1.1.2 - 2017-04-21 14 | - Fixed bug that occurred when the container element is not a direct parent of the element with `lazyFor` on it 15 | 16 | ## 1.1.1 - 2017-04-21 17 | - Fixed issue with uninitialized lists 18 | 19 | ## 1.1.0 - 2017-04-21 20 | - Added local variable named index that can be used to determine the index of the current item. e.g.: 21 | ```HTML 22 |
  • 23 | {{items}} {{i}} 24 |
  • 25 | ``` 26 | 27 | ## 1.0.0 - 2017-04-19 28 | - Initial release 29 | - Added all the things -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lazyFor 2 | 3 | `lazyFor` is an Angular 2+ directive that can be used in place of `ngFor`. The main difference is that `lazyFor` will only render items when they are visible in the parent element. So as a user scrolls, items that are no longer visible will be removed from the DOM and new items will be rendered to the DOM. 4 | 5 | ## Sample Usage 6 | ### [Plunker Demo](https://embed.plnkr.co/t9OKzEOObBClzI6MX6uo/?show=app.component.ts,preview) 7 | 8 | Install with `npm install --save angular-lazy-for` 9 | 10 | *app.module.ts* 11 | ```TypeScript 12 | import {NgModule} from '@angular/core'; 13 | import {LazyForModule} from 'angular-lazy-for'; 14 | 15 | @NgModule({ 16 | declarations: [/*...*/], 17 | imports: [ 18 | //... 19 | LazyForModule 20 | ], 21 | providers: [/*...*/], 22 | bootstrap: [/*...*/] 23 | }) 24 | export class AppModule { 25 | } 26 | ``` 27 | 28 | *Template Input* 29 | ```html 30 | 35 | ``` 36 | 37 | *DOM Output* 38 | ```html 39 | 46 | ``` 47 | 48 | ## When to use `lazyFor` 49 | * When you know the size of the iterable and you only want to create DOM elements for visible items 50 | * Fix performance issues with page load time 51 | * Fix change detection performance issues 52 | 53 | ## When *not* to use `lazyFor` 54 | * Not meant to replace `ngFor` in all cases. Only use `lazyFor` if you have performance issues 55 | * Not an infinite scroll. don't use it if you don't know the total size of the list 56 | * Doesn't currently support loading items asynchronously. Although support for this may be added in the future 57 | * This directive does some DOM manipulation so it won't work if your Angular app runs in a web worker or if you use Angular Universal 58 | 59 | ## Performance 60 | `lazyFor` can improve performance by preventing unnecessary content from being rendered to the DOM. This also leads to fewer bindings which reduces the load on change detection. Using `ngFor` is usually very fast but here is a casae where it has a noticeable performance impact: 61 | 62 | ### [Plunker Performance Demo](https://embed.plnkr.co/eRMjnhW1ctU1VwdRhE8x/?show=app.component.ts,preview) 63 | 64 | ## Optional Parameters 65 | 66 | ### withHeight 67 | This directive will try to figure out the height of each element and use that number to calculate the amount of spacing above and below the items. If you are having issues with the defualt behaviour you can specify an explicit height in pixels. 68 | 69 | ```HTML 70 |
    71 | ``` 72 | 73 | ### inContainer 74 | `lazyFor` needs to know which element is the scrollable container the items will be inside of. By default it will use the parent element but if this is not the right element you can explicitly specify the container. 75 | 76 | ```HTML 77 |
    78 |
    79 |
    80 |
    81 |
    82 | ``` 83 | 84 | ### withTagName 85 | This directive works by creating an empty element above and below the repeated items with a set height. By default these buffer elements will the use the same type of tag that `lazyFor` is on. However you can specify a custom tag name with this parameter if needed. 86 | 87 | *Template* 88 | ```HTML 89 |