{"id":571,"date":"2023-05-14T12:02:06","date_gmt":"2023-05-14T12:02:06","guid":{"rendered":"https:\/\/sitegator.in\/?p=571"},"modified":"2023-05-14T12:03:55","modified_gmt":"2023-05-14T12:03:55","slug":"directives-in-angular","status":"publish","type":"post","link":"https:\/\/sitegator.in\/cms\/directives-in-angular\/","title":{"rendered":"Directives in angular"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"548\" src=\"https:\/\/sitegator.in\/wp-content\/uploads\/2023\/05\/image-1.png\" alt=\"\" class=\"wp-image-574\" srcset=\"https:\/\/sitegator.in\/cms\/wp-content\/uploads\/2023\/05\/image-1.png 441w, https:\/\/sitegator.in\/cms\/wp-content\/uploads\/2023\/05\/image-1-241x300.png 241w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Steps to create directive<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/pratik-maurya\/Directives\/blob\/master\/README.md#step-1-create-an-angular-application-with-command\"><\/a>Step 1 create an angular application with command<\/h1>\n\n\n\n<p>ng new directiveApp<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/pratik-maurya\/Directives\/blob\/master\/README.md#step-2-create-an-directive-with-command\"><\/a>Step 2 create an directive with command<\/h1>\n\n\n\n<p>ng generate directive directiveName<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/pratik-maurya\/Directives\/blob\/master\/README.md#step-3-copy-and-paste-the-code-given-in-above-directivets-file\"><\/a>Step 3 copy and paste the code given in above directive.ts file.<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">directive.ts<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Directive,ElementRef,Input,HostListener } from '@angular\/core';\r\n@Directive({\r\n  selector: '&#91;appDirectives]'\r\n})\r\n\r\nexport class DirectivesDirective {\r\n\r\n  inputElement: ElementRef;\r\n\r\n  @Input('appDirectives')\r\n  appDirectives!: string;\r\n  arabicRegex = '&#91;\\u0600-\\u06FF]';\r\n\r\n  constructor(el: ElementRef) {\r\n    this.inputElement = el;\r\n  }\r\n\r\n  @HostListener('keypress', &#91;'$event']) onKeyPress(event: any) {\r\n    this.notAllowSpaceatFirst(event)\r\n  }\r\n\r\n  integerOnly(event: any) {\r\n    const e = &lt;KeyboardEvent>event;\r\n    if (e.key === 'Tab' || e.key === 'TAB') {\r\n      return;\r\n    }\r\n    if (&#91;'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'].indexOf(e.key) === -1) {\r\n      e.preventDefault();\r\n    }\r\n  }\r\n\r\n  noSpecialChars(event: any) {\r\n    const e = &lt;KeyboardEvent>event;\r\n    if (e.key === 'Tab' || e.key === 'TAB') {\r\n      return;\r\n    }\r\n    let k;\r\n    k = event.keyCode;  \/\/ k = event.charCode;  (Both can be used)\r\n    if ((k > 64 &amp;&amp; k &lt; 91) || (k > 96 &amp;&amp; k &lt; 123) || k === 8 || k === 32 || (k >= 48 &amp;&amp; k &lt;= 57)) {\r\n      return;\r\n    }\r\n    const ch = String.fromCharCode(e.keyCode);\r\n    const regEx = new RegExp(this.arabicRegex);\r\n    if (regEx.test(ch)) {\r\n      return;\r\n    }\r\n    e.preventDefault();\r\n  }\r\n\r\n  onlyChars(event: any) {\r\n    const e = &lt;KeyboardEvent>event;\r\n    if (e.key === 'Tab' || e.key === 'TAB') {\r\n      return;\r\n    }\r\n    let k;\r\n    k = event.keyCode;  \/\/ k = event.charCode;  (Both can be used)\r\n    if ((k > 64 &amp;&amp; k &lt; 91) || k === 8 || k === 32 || (k > 96 &amp;&amp; k &lt; 124)) {\r\n      return;\r\n    }\r\n    e.preventDefault();\r\n  }\r\n\r\n  allowDecimal(event: any) {\r\n    const e = &lt;KeyboardEvent>event;\r\n\r\n    if (e.key === 'Tab' || e.key === 'TAB') {\r\n      return;\r\n    }\r\n\r\n    let k;\r\n\r\n    k = event.keyCode;  \/\/ k = event.charCode;  (Both can be used)\r\n\r\n    if ((k == 48) || (k == 49) || (k == 50) || (k == 51) ||\r\n      (k == 52) || (k == 53) || (k == 54) || (k == 55) ||\r\n      (k == 56) || (k == 57)) {\r\n      var arcontrol = new Array();\r\n      var temp = this.inputElement.nativeElement.value;\r\n      arcontrol = this.inputElement.nativeElement.value.split(\".\");\r\n\r\n      if (arcontrol.length == 1) {\r\n        if (arcontrol&#91;0].length &lt; 16) {\r\n          return;\r\n        }\r\n        else {\r\n        }\r\n      }\r\n      else {\r\n        return;\r\n      }\r\n    }\r\n    else if (k == 46) {\r\n      var sCount = new Array();\r\n      sCount = this.inputElement.nativeElement.value.split(\".\");\r\n\r\n      if ((sCount.length) - 1 == 1) {\r\n      }\r\n      else {\r\n        return;\r\n      }\r\n    }\r\n\r\n    e.preventDefault();\r\n  }\r\n  notAllowSpaceatFirst(event: any) {\r\n    if (event.target.selectionStart === 0 &amp;&amp; event.code === \"Space\") {\r\n      event.preventDefault();\r\n    }\r\n    else {\r\n      if (this.appDirectives === 'integer') {\r\n        this.integerOnly(event);\r\n      } else if (this.appDirectives === 'noSpecialChars') {\r\n        this.noSpecialChars(event);\r\n      }\r\n      else if (this.appDirectives === 'onlyChars') {\r\n        this.onlyChars(event);        \r\n      }\r\n      else if (this.appDirectives === 'allowDecimal') {\r\n        this.allowDecimal(event);\r\n      }\r\n    }\r\n  }\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Component.ts<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1>Directives:&lt;\/h1>\r\n\r\n&lt;div>\r\n  &lt;p>Space At Start Not Allowed&lt;\/p>\r\n  &lt;input appDirectives=\"\" type=\"test\" placeholder=\"Enter text\">\r\n&lt;\/div>\r\n\r\n&lt;div>\r\n  &lt;p>Integers Only&lt;\/p>\r\n  &lt;input appDirectives=\"integer\" type=\"test\" placeholder=\"Enter text\">\r\n&lt;\/div>\r\n\r\n&lt;div>\r\n  &lt;p>Characters Only&lt;\/p>\r\n  &lt;input appDirectives=\"onlyChars\" type=\"test\" placeholder=\"Enter text\">\r\n&lt;\/div>\r\n\r\n&lt;div>\r\n  &lt;p>Special Characters Not Allowed&lt;\/p>\r\n  &lt;input appDirectives=\"noSpecialChars\" type=\"test\" placeholder=\"Enter text\">\r\n&lt;\/div>\r\n\r\n&lt;div>\r\n  &lt;p>Allow Decimal&lt;\/p>\r\n  &lt;input appDirectives=\"allowDecimal\" type=\"test\" placeholder=\"Enter text\">\r\n&lt;\/div><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Find source code on github<\/h2>\n\n\n\nhttps:\/\/github.com\/pratik-maurya\/Directives\/tree\/master\n","protected":false},"excerpt":{"rendered":"<p>Steps to create directive Step 1 create an angular application with command ng new directiveApp Step 2 create an directive with command ng generate directive directiveName Step 3 copy and paste the code given in above directive.ts file. directive.ts Component.ts Find source code on github https:\/\/github.com\/pratik-maurya\/Directives\/tree\/master<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-571","post","type-post","status-publish","format-standard","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/posts\/571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/comments?post=571"}],"version-history":[{"count":2,"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/posts\/571\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/posts\/571\/revisions\/575"}],"wp:attachment":[{"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/media?parent=571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/categories?post=571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sitegator.in\/cms\/wp-json\/wp\/v2\/tags?post=571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}