Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular directive to trim input

import { Directive, EventEmitter, Input, ChangeDetectorRef, Output, ElementRef, HostListener, Inject, Renderer2 } from '@angular/core';
import { NgModel } from '@angular/forms';

@Directive({
  selector: '[appTrim]'
})
export class TrimDirective {
  constructor(
    private renderer: Renderer2,
    private elementRef: ElementRef,
    private ngModel: NgModel) { }

  @HostListener("blur")
  onBlur() {
    let value = this.ngModel.model;

    if(value) {
      value = value.trim();
      this.renderer.setProperty(
        this.elementRef.nativeElement, "value", value);
      this.renderer.setAttribute(
        this.elementRef.nativeElement, "value", value);
      this.ngModel.update.emit(value);
    } else {
      this.renderer.setProperty(
        this.elementRef.nativeElement, "value", null);
      this.renderer.setAttribute(
        this.elementRef.nativeElement, "value", null);
      this.ngModel.update.emit("");
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: escape xss javascript 
Javascript :: import ipcrenderer in react 
Javascript :: radio button schema mongoose 
Javascript :: convert angular HTTP to Native HTTP in Ionic 
Javascript :: meteor create package 
Javascript :: angular-chart.js 
Javascript :: chrome version 
Javascript :: js check if object key exists 
Javascript :: useQuery by click 
Javascript :: backdrop issue with multiple modal 
Javascript :: useref in react hooks 
Javascript :: javascript sort two-dimensional array by column 
Javascript :: jquery event delegation 
Javascript :: javascript use class without instantiating 
Javascript :: convert c# to javascript online 
Javascript :: ex: splide carousel 
Javascript :: usestate hook with callback 
Javascript :: javascript add fields dynamically 
Javascript :: javascript timeline 
Javascript :: this.$set in vue 3 
Javascript :: react set initial state without constructor 
Javascript :: react-multi-carousel equal spacing issue 
Javascript :: swiper js 
Javascript :: difference node and npm 
Javascript :: sequelize db:create test environment 
Javascript :: updatedAt mongoose stop 
Javascript :: .keys() array 
Javascript :: indexof js 
Javascript :: jquery numeric validation 
Javascript :: quasar $t i18n 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =