Angular Block UI
npm install ng-if-block-uiA Block UI implementation for Angular



``bash`
npm install ng-if-block-ui --save
Include the BlockUIModule in your main app module.
`js
// All other imports
import { BlockUIModule } from 'ng-if-block-ui';
@NgModule({
imports: [
BlockUIModule.forRoot()
],
...
})
export class AppModule { }
`block-uiQuick Start
Wrap your main components in your app root template with a component.
Import the BlockUI decorator into your component and declare a property with the decorator.
This decorator will auto wire this property to the main Block UI instance of your app.
To start blocking your app, simply invoke the start method.stop
This method also can take a custom message to display while blocking.
Once finished call the method to stop blocking the app.
`js
import { Component } from '@angular/core';
// Import BlockUI decorator & optional NgBlockUI type
import { BlockUI, NgBlockUI } from 'ng-if-block-ui';
@Component({
selector: 'app-root',
template:
})
export class AppComponent {
// Decorator wires up blockUI instance
@BlockUI() blockUI: NgBlockUI;
constructor() {
this.blockUI.start('Loading...'); // Start blocking
setTimeout(() => {
this.blockUI.stop(); // Stop blocking
}, 2000);
}
`
method. The default message will be shown any time blocking is activated.| Setting | Type | Description |
|-----------|----------|------------------------------------------------|
|
message | string | Custom message to be displayed while blocking. |#### Module Level
`ts
@NgModule({
imports: [
BlockUIModule.forRoot({
message: 'Default Message'
})
],
...
})
export class AppModule { }
`#### Component Level
`html
`#### Method Level
`js
@Component({
...,
template:
})
export class Cmp {
@BlockUI() blockUI: NgBlockUI; defaultMessage() {
this.blockUI.start(); // "Default Message" will display
}
customMessage() {
this.blockUI.start('Updating...'); // "Updating..." will display
}
}
`$3
When blocking with fast service calls the block overlay can flicker for a small amount of time.
To prevent this a delayStart and a delayStop can be configured to prevent this scenario.| Setting | Type | Description |
|--------------|----------|-------------------------------------------------------------------|
|
delayStart | number | Waits given amount of milliseconds before starting to block. |
| delayStop | number | Waits given amount of milliseconds before stopping current block. |#### Module Level
`ts
@NgModule({
imports: [
BlockUIModule.forRoot({
delayStart: 500,
delayStop: 500
})
],
...
})
export class AppModule { }
`#### Component Level
`html
`
$3
If you want to display other markup than the default spinner and message then you can provide a custom template.
Custom templates can be provided as a Component or TemplateRef. The template will then be used instead of the default template whenever blocking.| Setting | Type | Description |
|------------|-------------------------------------------|------------------------------------------|
|
template | Component | TemplateRef | Custom template to be used when blocking |#### Component Custom Template
Create a component and declare it in your app module.
The component also will need to be added to the
entryComponents property of the module.Example Component:
*Note: When providing a
Component as a template just add the {{message}}
interpolation to your template and it will display your default message or the message passed to the start method.*`js
// Template component
// Use block-ui-template class to center div if desired
@Component({
template: 
{{message}}
})
export class BlockTemplateCmp {}
`##### Module Level
`js
@NgModule({
imports: [
BlockUIModule.forRoot({
template: BlockTemplateCmp
})
],
declarations: [
...,
BlockTemplateCmp // Declare template component
],
entryComponents: [ BlockTemplateCmp ]
})
export class AppModule { }
`##### Component Level
`js
@Component({
selector: 'app-root',
template:
})
export class AppComponent {
// Declare template component
blockTemplate: BlockTemplateCmp = BlockTemplateCmp;
}
`#### TemplateRef Custom Template
Add a
with a template reference variable to the view. Then pass the template reference variable to the blockUI component using the [template] property.Note: TemplateRef templates can only be set on a Component level.
##### Component Level
`js
@Component({
selector: 'cmp',
template: 
`
})
export class Cmp {}
Add the *blockUI structural directive to any element*blockUI="'contact-list'"
and pass it an instance name .
Then in a component create a class property using the Block UI decorator with the instance name @BlockUI('contact-list'). This will then take care of wiring up that property to point to that specific instance in your app.
`js
@Component({
selector: 'app-cmp',
template:
})
export class AppComponent {
// Pass instance name to decorator
@BlockUI('contact-list') blockUIList: NgBlockUI; constructor() {
this.blockUIList.start('Loading...'); // Start blocking element only
this.blockUIList.stop(); // Stop blocking
}
`$3
Angular has a specific syntax for passing properties to structural directives. Properties are passed in key: value; pair. To pass settings to a Block UI directive they must be passed as shown below.`html
`NgBlockUI Instance
Below highlights all the properties that can be found on a BlockUI instance when a class property is decorated with the @BlockUI() decorator.| Property | Description |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
name | Name of the targeted instance (defaults to main instance). |
| isActive | Indicates if the targeted instance is blocking. |
| start | Starts blocking for instance, can be passed an optional message. |
| stop | Stops blocking for instance. |
| reset | Stops blocking for all currently blocking instances app wide regardless of the delayStop option. |
| update | Updates current instances blocking message with the passed message. |
| unsubscribe | Unsubscribe an instance so it no longer can be blocked. All BlockUI components/directives unsubscribe by default during onDestroy |BlockUIService
In some cases you may want to have more control over all the instances in you app.
Instead of declaring seperate instances with the @BlockUI() decorator you can use the BlockUIService. This service allows you to easily target multiple instance across your app.| Method | Parameters | Description |
|---------------|------------------------------------------------------------|------------------------------------------------------------------------------------------|
|
isActive | target: string | string[] | Indicates if the targeted instance(s) is blocking. |
| start | target: string | string[], message?: any | Starts blocking for a single instance or multiple instances by passing instance name(s). |
| stop | target: string | string[] | Stops blocking for a single instance or multiple instances by passing instance name(s). |
| reset | target: string | string[] | Resets blocking for a single instance or multiple instances by passing instance name(s). |
| unsubscribe` | target: string | string[] | Unsubscribes a single instance or multiple instances by passing instance name(s). |