10.0.1 • Published 9 months ago

ngx-data-loader v10.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

NgxDataLoader

Lightweight Angular component to simplify asynchronous data loading.

Build status NPM version NPM downloads MIT license Minzipped size CodeFactor Codecov

Demo

⚡️ View demo on StackBlitz

Installation

Install the package:

npm install ngx-data-loader

Import the module:

import { NgxDataLoaderModule } from "ngx-data-loader";

@NgModule({
  imports: [
    NgxDataLoaderModule,
    // ...
  ],
  // ...
})
export class AppModule {}

Usage

Basic Example

<!-- app.component.html -->
<ngx-data-loader [loadFn]="getTodos">
  <ng-template #loading>Loading todos...</ng-template>
  <ng-template #error let-error
    >Failed to load todos. {{ error.message }}</ng-template
  >
  <ng-template #loaded let-todos>
    @for (todo of todos; track todo.id) {
      <div>
        Title: {{ todo.title }}<br />
        Completed: {{ todo.completed ? "Yes" : "No" }}
      </div>
    }
  </ng-template>
</ngx-data-loader>
/* app.component.ts */
@Component({
  // ...
})
export class AppComponent {
  getTodos = () => this.http.get("https://jsonplaceholder.typicode.com/todos");

  constructor(private http: HttpClient) {}
}

Reloading Data

<!-- app.component.html -->
<button (click)="todosLoader.reload()">Reload Todos</button>

<ngx-data-loader [loadFn]="getTodos" #todosLoader>
  <!-- Loading and Error templates -->
  <!-- ... -->
  <ng-template #loaded let-todos>
    <!-- Content here -->
    <!-- ... -->
  </ng-template>
</ngx-data-loader>

Loading Data Based on Route Parameters

<!-- app.component.html -->
<ngx-data-loader [loadFn]="getTodo" [loadFnArgs]="route.params | async">
  <ng-template #loading>Loading todo...</ng-template>
  <ng-template #error>Failed to load todo.</ng-template>
  <ng-template #loaded let-todo>
    Title: {{ todo.title }}<br />
    Completed: {{ todo.completed ? 'Yes' : 'No' }}
  </ng-template>
</ngx-data-loader>
/* app.component.ts */
@Component({
  // ...
})
export class AppComponent {
  getTodo = ({ id }: { id: string }) =>
    this.http.get(`https://jsonplaceholder.typicode.com/todos/${id}`);

  constructor(
    private http: HttpClient,
    public route: ActivatedRoute,
  ) {}
}

Loading Data Based on Search Input

⚡️ Open on StackBlitz

⚡️ View advanced demo on StackBlitz

<!-- app.component.html -->
<h1>Search</h1>
<input ngModel #searchbox placeholder="Search" />
<ngx-data-loader
  [loadFn]="searchProducts"
  [loadFnArgs]="searchbox.value"
  [debounceTime]="300"
>
  <ng-template #loading>Searching...</ng-template>
  <ng-template #error>Error</ng-template>
  <ng-template #loaded let-results>
    <h2>{{ results.total }} search results for "{{ searchbox.value }}"</h2>
    @for (product of results.products; track product.id) {
      <div>
        <h3>{{ product.title }}</h3>
        <p>{{ product.description }}</p>
      </div>
    }
  </ng-template>
</ngx-data-loader>
/* app.component.ts */
@Component({
  // ...
})
export class AppComponent {
  searchProducts = (keywords: string) =>
    this.http.get("https://dummyjson.com/products/search", {
      params: { q: keywords },
    });

  constructor(private http: HttpClient) {}
}

Template Slots

NameDescriptionTemplate Context
loadedTemplateTemplate shown when data has loaded (#loaded)$implicit: T - the loaded dataloading: boolean - true if data is reloading (only if showStaleData is true)
loadingTemplateTemplate shown when data is loading (#loading)(none)
errorTemplateTemplate shown when data failed to load (#error)$implicit: Error - the error objectretry: () => void - function to retry loading data

Properties

NameDescription
loadFn: () => Observable<T>Function that returns an Observable of the data to load. Called on init and on reload.
loadFnArgs?: anyArguments to pass to loadFn. Changes trigger a reload.
initialData?: TData to display on init. If set, loadFn is not called on init.
debounceTime: numberMilliseconds to debounce reloads.
showStaleData: booleanKeep displaying previous data while reloading. Default is false.
loadingTemplateDelay: numberDelay before showing the loading template (in milliseconds). Default is 0.

Methods

NameDescription
reload()Resets the loading state and calls loadFn.
cancel()Cancels the pending loadFn and aborts related HTTP requests.
setData(data: T)Updates the state as if data was loaded through loadFn.
setError(error: Error)Updates the state as if an error occurred in loadFn.

Events

NameDescription
dataLoadedEmits when data loads successfully.
loadAttemptStartedEmits when data loading starts.
loadAttemptFailedEmits when data fails to load.
loadAttemptFinishedEmits when loading finishes (success or failure).
loadingStateChangeEmits the entire loading state when it changes.

Interfaces

interface LoadingState<T> {
  loading: boolean;
  loaded: boolean;
  error?: Error;
  data?: T;
}

FAQ

How to get type safety for the data loaded by NgxDataLoaderComponent?

Angular doesn't currently support type inference for template variables. To get type safety, you can use a presentational component inside the #loaded template that takes the data as a typed input.

Example:

<!-- app.component.html -->
<ngx-data-loader [loadFn]="getTodos">
  <!-- ... -->
  <ng-template #loaded let-todos>
    <app-todo-list [todos]="todos"></app-todo-list>
  </ng-template>
</ngx-data-loader>
// todo-list.component.ts
@Component({
  // ...
})
export class TodoListComponent {
  @Input() todos: Todo[];
}

This ensures type safety within your component's template.

If you need template type inference, consider using ngx-load-with. Its API is similar and supports type inference.

Contributing

Please read CONTRIBUTING.md.

License

The MIT License (MIT). See License File for details.

Contact

For issues or questions, please use the GitHub issues page.

10.0.1

9 months ago

10.0.0

1 year ago

7.1.0-beta.0

2 years ago

7.1.0-beta.2

2 years ago

7.1.0-beta.3

2 years ago

7.1.3

2 years ago

7.1.2

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

8.0.0-beta.5

2 years ago

8.0.0-beta.0

2 years ago

8.0.0-beta.2

2 years ago

8.0.0-beta.1

2 years ago

8.0.0-beta.4

2 years ago

8.0.0-beta.3

2 years ago

6.0.2

2 years ago

7.0.0

2 years ago

7.0.1

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.3

2 years ago

8.0.2

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

5.0.8

2 years ago

5.0.7

2 years ago

5.0.6

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

4.0.2

2 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.5

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago