Topic: Angular input label not working correctly
Daniel Braun
priority
asked 8 years ago
<div class="md-form">
<input mdbActive id="valve" type="number" class="form-control">
<label for="valve" class="">Valve</label>
</div>
Adrian Sawicki
free
answered 8 years ago
import {Directive, ElementRef, Output, HostListener, Renderer, AfterViewInit} from '@angular/core';
@Directive({
selector: '[mdbActive]'
})
export class ActiveDirective implements AfterViewInit {
public el: ElementRef = null;
public elLabel: ElementRef = null;
public elIcon: Element = null;
constructor(el : ElementRef, public renderer: Renderer) {
this.el = el;
}
@HostListener('focus', ['$event']) onClick() {
this.initComponent();
}
@HostListener('blur', ['$event']) onBlur() {
this.checkValue();
}
ngAfterViewInit() {
this.initComponent();
this.checkValue();
}
private initComponent(): void {
// this.el.nativeElement = event.target;
let inputId;
let inputP;
try {
inputId = this.el.nativeElement.id;
} catch(err) {}
try {
inputP = this.el.nativeElement.parentNode;
} catch(err) {}
this.elLabel = inputP.querySelector('label[for="'+ inputId +'"]') || inputP.querySelector('label');
if(this.elLabel != null)
this.renderer.setElementClass(this.elLabel, 'active', true);
this.elIcon = inputP.querySelector('i') || false;
if(this.elIcon) {
this.renderer.setElementClass(this.elIcon, 'active', true);
}
}
private checkValue(): void {
let value = '';
if(this.elLabel != null) {
value = this.el.nativeElement.value || '';
if(value === '') {
this.renderer.setElementClass(this.elLabel, 'active', false);
if(this.elIcon) {
this.renderer.setElementClass(this.elIcon, 'active', false);
}
}
}
}
}
5. Should work now
Bartholomew Smyth
free
answered 8 years ago
Edyta Dabrowska
free
answered 8 years ago
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
- ForumUser: Priority
- Premium support: Yes
- Technology: MDB Angular
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No