{"id":1289,"date":"2023-08-08T05:00:00","date_gmt":"2023-08-08T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1289"},"modified":"2023-08-07T16:03:53","modified_gmt":"2023-08-07T23:03:53","slug":"how-to-create-custom-directives","status":"publish","type":"post","link":"http:\/\/www.angulartraining.com\/daily-newsletter\/how-to-create-custom-directives\/","title":{"rendered":"How to create custom directives?"},"content":{"rendered":"\n<p>I&#8217;ve touched on <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/when-to-create-a-directive-vs-a-component\/\" target=\"_blank\" rel=\"noopener\" title=\"\">when it makes sense to create custom directives<\/a> ,  <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/hostbinding-and-hostlistener\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to use bindings in directives<\/a>, and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/the-power-of-angular-selectors\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to get creative with Angular selectors<\/a>. <\/p>\n\n\n\n<p>Today, let&#8217;s tackle how to create custom directives. The first thing to do is to use the Angular CLI:<\/p>\n\n\n\n<p><code>ng generate directive [name]<\/code> <\/p>\n\n\n\n<p>or <code>ng g d [name]<\/code><\/p>\n\n\n\n<p>It&#8217;s also possible to generate <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/what-are-standalone-components\/\" target=\"_blank\" rel=\"noopener\" title=\"\">standalone directives<\/a> with the <code>--standalone<\/code> option:<\/p>\n\n\n\n<p><code>ng generate directive [name] --standalone<\/code><\/p>\n\n\n\n<p>Once you&#8217;ve done that, you end up with an empty class to implement:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"262\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-3-1024x262.png\" alt=\"\" class=\"wp-image-1291\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-3-1024x262.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-3-300x77.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-3-768x196.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-3-1536x393.png 1536w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-3.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>At that point, it&#8217;s important to remember that a directive is just like a component. The only difference is that a directive doesn&#8217;t have an HTML template. As a result, we can still use <code>@Input()<\/code>, <code>@Output()<\/code>, and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/hostbinding-and-hostlistener\/\" target=\"_blank\" rel=\"noopener\" title=\"\">all sorts of bindings and listeners<\/a>.<\/p>\n\n\n\n<p>As a result, your directive will manipulate HTML attributes and events on its host element. Here is a typical example of a custom directive that sets a credit card logo image based on a credit card number:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-4-1024x100.png\" alt=\"\" class=\"wp-image-1293\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-4-1024x100.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-4-300x29.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-4-768x75.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-4-1536x151.png 1536w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-4.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Its source code looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"927\" height=\"1024\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-5-927x1024.png\" alt=\"\" class=\"wp-image-1294\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-5-927x1024.png 927w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-5-271x300.png 271w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-5-768x849.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-5-1390x1536.png 1390w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-5.png 1714w\" sizes=\"auto, (max-width: 927px) 100vw, 927px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As you can see, we use an <code>@Input()<\/code> to receive the <strong>cardNumber <\/strong>and a host binding to change the value of the <code>src<\/code> attribute. You can find a <a href=\"https:\/\/blog.angulartraining.com\/tutorial-how-to-create-your-own-angular-directive-3532d7f31fab\" target=\"_blank\" rel=\"noopener\" title=\"\">step-by-step tutorial<\/a> that explains the above code in more detail <a href=\"https:\/\/blog.angulartraining.com\/tutorial-how-to-create-your-own-angular-directive-3532d7f31fab\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve touched on when it makes sense to create custom directives , how to use bindings in directives, and how to get creative with Angular selectors. Today, let&#8217;s tackle how to create custom directives. The first thing to do is to use the Angular CLI: ng generate directive [name] or ng g d [name] It&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,11],"tags":[],"class_list":["post-1289","post","type-post","status-publish","format-standard","hentry","category-angular","category-directives"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1289"}],"version-history":[{"count":4,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1289\/revisions"}],"predecessor-version":[{"id":1296,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1289\/revisions\/1296"}],"wp:attachment":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1289"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}