angularjs - directive doesn't function properly -


i'm trying write directive limits input of user. on example limit-directive="2", user can type 2 characters in input field.

my problem directive not working. invoked doesn't stop key press return false or event.preventdefault();. ideas on missing ?

export class limitdirective implements angular.idirective {         restrict = 'a';          constructor() {             if(console) console.log('limit directive invoked')         }          link = (scope: angular.iscope, element: angular.iaugmentedjquery, attrs: angular.iattributes) => {             var limit = parseint(attrs.limitdirective);              angular.element(element).on("keypress", function(event) {                 if (this.value.length >= limit){                     var allowedkeys = [8, 9, 13, 35, 36, 37, 38, 39, 40, 46];                     var key = event.which || event.keycode;                      if (allowedkeys.indexof(key) < 0 ){                         event.preventdefault();                     }                  }             });         }          static factory(): angular.idirectivefactory {             const directive = () => new limitdirective();             return directive;         }     } 

i think want use if less or equals operator instead of greater or equals. , doubt this correspond element in callback of event.. suggest do:

 element.bind("keypress", function(event) {                 if (element[0].value.length <= limit){                     var allowedkeys = [8, 9, 13, 35, 36, 37, 38, 39, 40, 46];                     var key = event.which || event.keycode;                 if (allowedkeys.indexof(key) < 0 ){                     event.preventdefault();                 }              }         }); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -