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

@Directive({
  selector: '[mpnAadharDirective]'
})
export class GeneralDirective {

  constructor(private el: ElementRef) { }

  @HostListener('keyup') keyup() {
    console.log(this.el.nativeElement.value.replace(/\d(?=\d{4})/g, "*"))
  }
}

With the above code I'm trying to replace the nativeElement value, but I get the value printed but it is not replacing. I need some help in fixing this. Thank you.

2
  • 1
    the replace: true flag had come up with more problems than solutions which is why it was removed. therefore you can not build directives in such a way anymore and provide valid html markup. The only workaround is to use an attribute-directive instead: Commented Jun 6, 2019 at 12:37
  • Thank you, should we use @input as workaround? Commented Jun 6, 2019 at 12:56

1 Answer 1

2

Replacing event target value should works:

@HostListener('keyup', ['$event']) keyup(event) {
  event.target['value'] = this.el.nativeElement.value.replace(/\d(?=\d{4})/g, "*");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. When I console this out, it's only printing the values I type and not replacing. That's the issue.
I'm not sure about your problem, this is a small Stackblitz example, it seems to work.
Wow, thanks. I had some issue with ngx mask which was interfering.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.