Skip to content

Instantly share code, notes, and snippets.

@leifermendez
Last active March 15, 2021 11:23
Show Gist options
  • Save leifermendez/dda2d122fe00fdcfbc34ad2e66a2d220 to your computer and use it in GitHub Desktop.
Save leifermendez/dda2d122fe00fdcfbc34ad2e66a2d220 to your computer and use it in GitHub Desktop.
loading-btn.directive.ts
import {Directive, ElementRef, Input, OnChanges, OnInit, Renderer2} from '@angular/core';
@Directive({
// tslint:disable-next-line:directive-selector
selector: '[ngxLoading]'
})
export class LoadingBtnDirective implements OnInit, OnChanges {
@Input() textLoading: string;
@Input() textInitial: string;
@Input() disabled: boolean;
@Input() loadingFlag: boolean | undefined = undefined;
constructor(private elem: ElementRef) {
}
ngOnInit(): void {
}
ngOnChanges(changes): void {
if (changes.condition && changes.condition.currentValue) {
this.loadingFlag = changes.condition.currentValue;
}
this.elem.nativeElement.innerText = (this.loadingFlag) ? this.textLoading : this.textInitial;
if (![undefined].includes(this.loadingFlag)) {
this.elem.nativeElement.disabled = !!(this.disabled);
}
}
}
@leifermendez
Copy link
Author

Example of use:

Attribute Default Objetive
textInitial string: '' Text initial
textLoading string: '' Text when loading
loadingFlag boolean: false swith loading

typescript

public loading:boolean

html

<!-- ********* SAMPLE WITH TRANSLATE ************ --->
     <button class="btn btn-primary btn-sm"
              ngxLoading [loadingFlag]="loading" [textInitial]="'GENERAL.SEND' | translate"
              [textLoading]="'GENERAL.SENDING' | translate">

<!-- ********* SAMPLE WITHOUT TRANSLATE ************ --->
     <button class="btn btn-primary btn-sm"
              ngxLoading [loadingFlag]="loading" [textInitial]="'Continue'"
              [textLoading]="'Sending...'">

When loading is false
image

When loading is true
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment