Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
ngx-data-loader
Advanced tools
Simplify async data loading in Angular with NgxDataLoaderComponent.
NgxDataLoaderComponent
simplifies asynchronous data loading in Angular by abstracting away tedious tasks such as error handling, cancel/reload strategies, and template display logic. This helps you focus on the core functionality of your application.
A key feature of NgxDataLoaderComponent
is its ability to handle dynamic arguments through the use of loadFnArgs
. This feature enables you to pass in dynamic arguments to loadFn
, and each time the value changes, loadFn
is triggered with the new arguments. This makes it easy to load data based on route parameters or the value of an input field.
To use the component, provide a loadFn
that returns an Observable
of the data, and optional templates for each loading phase.
reload
and cancel
methodsInstall the package
npm install ngx-data-loader
Import the module
import { NgxDataLoaderModule } from 'ngx-data-loader';
@NgModule({
imports: [
NgxDataLoaderModule,
...
],
...
})
export class AppModule {}
<!-- app.component.html -->
<ngx-data-loader [loadFn]="getTodo">
<ng-template #loading> Loading todos... </ng-template>
<ng-template #error> Failed to load todos. </ng-template>
<ng-template #loaded let-todos>
<div *ngFor="let todo of todos">
Title: {{ todo.title }} <br />
Completed: {{ todo.completed ? 'Yes' : 'No' }}
</div>
</ng-template>
</ngx-data-loader>
/* app.component.ts */
@Component({
...
})
export class AppComponent {
getTodo = () => this.http.get('https://jsonplaceholder.typicode.com/todos');
constructor(private http: HttpClient) {}
}
<!-- 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: string}) => this.http.get(`https://jsonplaceholder.typicode.com/todos/${id}`);
constructor(private http: HttpClient, public route: ActivatedRoute) {}
}
Name | Description | Template outlet context |
---|---|---|
@ContentChild('loaded') loadedTemplate?: TemplateRef<unknown> | Template to be displayed when the data has loaded. | $implicit: T : the resolved data.loading: boolean : whether the data is reloading (only available if showStaleData is set to true ). |
@ContentChild('loading') loadingTemplate?: TemplateRef<unknown> | Template to be displayed when the data is loading. | (none) |
@ContentChild('error') errorTemplate?: TemplateRef<unknown> | Template to be displayed when the data failed to load. | $implicit: Error<unknown> : the error object.retry: () => void : can be called to trigger a retry. |
Name | Description |
---|---|
@Input() loadFn!: () => Observable<T> | Function that returns an Observable of the data to be loaded. Called on init and on reload. |
@Input() loadFnArgs?: any | Arguments to pass to loadFn . Changes to this property will trigger a reload. |
@Input() initialData?: T | Data to be rendered on init. When set, loadFn will not be invoked on init. The loading state will be set to loaded . |
@Input() debounceTime: number | Number of milliseconds to debounce reloads. |
@Input() retries: number | Number of times to retry loading the data. Default: 0 |
@Input() retryDelay: number | Delay in milliseconds between retries. Default: 1000 |
@Input() showStaleData: boolean | Whether to keep displaying previously loaded data while reloading. Default: false |
@Input() loadingTemplateDelay: number | Delay in milliseconds before showing the loading template. Default: 0 |
@Input() timeout?: number | Number of milliseconds to wait for loadFn to emit before throwing an error. |
Name | Description |
---|---|
@Output() dataLoaded: EventEmitter<T> | Emits when the data is loaded. |
@Output() loadAttemptStarted: EventEmitter<void> | Emits when the data loading is started. |
@Output() loadAttemptFailed: EventEmitter<Error> | Emits when the data fails to load. |
@Output() loadAttemptFinished: EventEmitter<void> | Emits when the data has either loaded or failed to load. |
@Output() loadingStateChange: EventEmitter<LoadingState<T>> | Emits entire loading state when any of the above events are emitted. |
Name | Description |
---|---|
reload: () => void | Resets the loading state and calls the loadFn that you provided. |
cancel: () => void | Cancels the pending loadFn and aborts any related http requests1. |
setData: (data: T) => void | Updates the loading state as if the passed data were loaded through loadFn . |
setError: (error: Error) => void | Updates the loading state as if the passed error were thrown by loadFn . |
interface LoadingState<T> {
loading: boolean;
loaded: boolean;
error?: Error;
data?: T;
}
The MIT License (MIT). Please see License File for more information.
FAQs
Lightweight Angular component to simplify asynchronous data loading.
The npm package ngx-data-loader receives a total of 4 weekly downloads. As such, ngx-data-loader popularity was classified as not popular.
We found that ngx-data-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.