Created
March 10, 2020 15:43
-
-
Save bgilbert6/5e08afac6806719271ac1db767b39523 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, Input, Renderer2, ElementRef, ViewChild } from '@angular/core'; | |
@Directive({ | |
selector: '[btnLoading]' | |
}) | |
export class BtnLoadingDirective { | |
@Input() | |
set btnLoading(condition: boolean) { | |
if(condition == true) { | |
const icon2: HTMLElement = this.renderer.createElement("i"); | |
icon2.className = "fas fa-spinner fa-spin"; | |
this.renderer.addClass(this.el.nativeElement, 'btn-loading'); | |
this.renderer.setAttribute(this.el.nativeElement,"disabled", "disabled"); | |
this.renderer.insertBefore(this.el.nativeElement, icon2, this.el.nativeElement.firstChild) | |
} else { | |
this.renderer.removeClass(this.el.nativeElement, 'btn-loading'); | |
this.renderer.removeAttribute(this.el.nativeElement,"disabled"); | |
const icon: HTMLElement = this.el.nativeElement.querySelector("i"); | |
if(icon != undefined) { | |
this.renderer.removeChild(this.el.nativeElement, icon) | |
} | |
} | |
} | |
constructor(private el: ElementRef, | |
private renderer: Renderer2) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment