1

I have the following directive groupingFormat which perform grouping to an input text when user use the key up:

@Directive({
  selector: '[groupingFormat]'
})
export class GroupingFormatDirective {

  private el: HTMLInputElement;  
  constructor(elRef: ElementRef) {
    this.el = elRef.nativeElement;
  }

  ngAfterViewInit(): void {
    let elem : HTMLInputElement = this.el; 
     elem.addEventListener('keyup',() => { 
      this.el.value = this.digitGrouping(this.el.value);
     });
   }
}

Example of usage:

<input type="text" #myValue="ngModel" name="my_value" [(ngModel)]="myObj.myValue" id="my_value" required groupingFormat>

This directive is working as expected but now I have new requirement: The input text should use the directive also when the page is load and also if the a form is open inside the page with the input becoming visible.

Is there an easy way to update the directive to support this functionality or any alternative solution? Attach another directive ? Thanks.

5
  • What do you mean by input text should use directive? Commented Mar 6, 2019 at 6:33
  • The directive is not working when the form is visible that's all because I have only event for keyup. Commented Mar 6, 2019 at 6:37
  • Did you tried using hostlistener decorator? Commented Mar 6, 2019 at 6:40
  • no, if you think that's the solution for the problem can you try answer the question? thanks Commented Mar 6, 2019 at 6:42
  • Already answered by @Kshitij Try That Commented Mar 6, 2019 at 6:44

1 Answer 1

1
<input type="text" name="my_value" [appInputevent]="myValue" [(ngModel)]="myValue">

directive file

import { Directive,HostListener,ElementRef, Input } from '@angular/core';

@Directive({
  selector: '[appInputevent]'
})
export class InputeventDirective {

  constructor(private el:ElementRef) { }

  @Input('appInputevent') params: string;

 @HostListener('keyup', ['$event'])
  onKeyUp(event: KeyboardEvent) {
    console.log('got parameters: '+this.params);
  }

  private highlight(color: string) {
    this.el.nativeElement.style.backgroundColor = color;
  }

}

change hostlistner event according to your need.

For demonstration-- https://stackblitz.com/edit/ionic2-test?file=app%2Finputevent.directive.ts

Sign up to request clarification or add additional context in comments.

7 Comments

I've tried this solution and I'm getting now: Can't bind to 'appInputevent' since it isn't a known property of 'input'
i have added link for demonstration. check only for InputeventDirective, as there is one more directive.
I've removed the brackets [] and now no errors, but It doesn't work.
don't remove bracket. check if you have imported directive properly. have you went thorough the link?
myvalue variable needs to be declared also.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.