Search code examples
angularangular6angular-directiveangular-pipe

Angular number format on forms on blur and on load by using directive, Also the formControl data should not be formatted


I am trying to number format the form input field, where the number should be formatted when on load and on blur. on focus to the field i will convert it to normal text. also the value in the formgroup should not be in formatted.

App.html

<div>
<form [formGroup]='rForm' (ngSubmit)= "addPost(rForm.value)">
  <div class="form-container" >
    <div class="row columns" >
      <h1>My Reactive form</h1>

      <label>Name
        <input type="text" formControlName="name" appDecimalFormat >
      </label>

      <label>Description
        <textarea formControlName="description"></textarea>
      </label> 

      <label for="validate">Minimum of 3 characters</label>
      <input type="checkbox" name="validate" formControlName="validate" value="1">On

      <input type="submit" class="button expnded" value="Submit Form" [disabled]="!rForm.valid">
    </div>
  </div>
</form>
</div>

<ng-template #forminfo>
  <div class="form-container">
    <div class="row columns">
      <h1>{{name}}</h1>

      <p>{{ description }}</p>
      <input type="button" (click)=""
    </div>
    </div> 
</ng-template>

app.ts

import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  rForm: FormGroup;
  post:any;
  description:any;
  name:any;

  constructor(private fb:FormBuilder){
      this.rForm = fb.group({
        'name':[null, Validators.required],
        'description':[null, Validators.compose([Validators.required,Validators.minLength(3),Validators.maxLength(5)])],
        'validate' :'',
      })
  }

  addPost(post){
    this.description = post.description;
    this.name = post.name;
  }

}

number-format-directive.ts

i am using this directive to format the number please help me to sort out this.

import { Directive, HostListener, HostBinding, Renderer2, ElementRef } from '@angular/core';
import {DecimalPipe} from '@angular/common';

@Directive({
  selector: '[appDecimalFormat]'
})
export class DecimalFormatDirective {

  constructor(private decimalPipe: DecimalPipe, private renderer: Renderer2, private el: ElementRef) {
}
  @HostListener('focus') onFocus($event){
    console.log(event.target.value)
     this.backgroundColor = 'silver';
     this.renderer.setAttribute(this.el.nativeElement,'input', this.decimalPipe.transform(event.target.value));

  }
  @HostListener('blur') onBlur($event){
    console.log(event.target.value)
     this.backgroundColor = 'white';
     console.log(this.decimalPipe.transform(event.target.value));
      this.renderer.setAttribute(this.el.nativeElement,'input', this.decimalPipe.transform(event.target.value));
  }

  @HostBinding('style.background-color') get setColor(){
    return this.backgroundColor;
  }
  private backgroundColor = 'white';


}

Solution

  • Injecting pipe is not a good thing to do. Instead you can use functions exported from the @angular/common package. For eg, use formatNumber can be used. more

    You can set the value back to input box as follows

    this.el.value = formatNumber(num); 
    

    formatNumber() is a fn exported from @angular/common package.