Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

javascript - Custom Directive developed in Angular 9 not working in mobiles

I have developed a custom directive in angular 9 to enter only digits and 1 decimal. The directive works flawlessly in desktop but not in mobiles (as if the directive is not even present in the input field). I used keydown event as the host. Is this an issue in Angular side? Any suggestions or existing posts that I need to refer.

EDIT

@Directive({ 
    selector: 'input[allowCurr]',
    host: { '(keydown)': 'allowND($event)' } 
})
export class AllowCurrDirective { 
    @Input() allowCurr: string;
     /* Rest of my Logic here */ 
} 

I added this much because I had put alert inside my allowND function, it is going inside it but event.key, that I had used to capture user key input, is coming undefined.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

May you could try with HostListener

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

@Directive({
  selector: "input[allowCurr]"
})
export class AllowCurrDirective {
  @HostListener("keydown", ["$event"])
  allowND(e) {
    console.log(e.key);
  }
  @Input() allowCurr: string;
  /* Rest of my Logic here */
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...