{"id":1611,"date":"2023-10-11T05:00:00","date_gmt":"2023-10-11T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1611"},"modified":"2023-10-02T15:20:14","modified_gmt":"2023-10-02T22:20:14","slug":"directive-composition-api","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/directive-composition-api\/","title":{"rendered":"Directive Composition API"},"content":{"rendered":"\n<p>In the past few weeks, we&#8217;ve covered different examples of directives, such as the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/using-the-cdk-listbox-for-custom-list-selection\/\" target=\"_blank\" rel=\"noopener\" title=\"Using the CDK ListBox for custom list selection\">CDK Listbox directive<\/a> and the CDK Drag and Drop directive. I have mentioned that <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/when-to-create-a-directive-vs-a-component\/\" title=\"When to create a directive vs. a component?\">directives are underused<\/a> in Angular applications these days, and one way to use more directives is to adopt the <a href=\"https:\/\/angular.io\/guide\/directive-composition-api\" target=\"_blank\" rel=\"noopener\" title=\"\">directive composition API<\/a>.<\/p>\n\n\n\n<p>Let&#8217;s consider our previous example of a list of items:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"289\" height=\"159\" src=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-53.png\" alt=\"\" class=\"wp-image-1550\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Let&#8217;s assume we want to make our list flexible enough to support re-ordering items using drag and drop. To do so, we could start using multiple different directives on the same element, such as <code>cdkOption<\/code> and <code>cdkDrag<\/code> (<a href=\"https:\/\/stackblitz.com\/edit\/at-before-composition?file=src%2Fexample%2Fcdk-listbox-custom-typeahead-example.html\" target=\"_blank\" rel=\"noopener\" title=\"\">code example here on Stackblitz<\/a>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"274\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image.png\" alt=\"\" class=\"wp-image-1612\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image.png 593w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-300x139.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>While the above code works, it&#8217;s designed for a one-time use case. If we know that our application will use several droplists that support drag-and-drop, we should start thinking about creating our own custom directive that refactors these different features in one place. Enter the directive composition API:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"468\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-1.png\" alt=\"\" class=\"wp-image-1613\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-1.png 488w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-1-300x288.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This new directive <code>draggableOption<\/code> is composed of both <code>cdkOption<\/code> and <code>cdkDrag<\/code> to achieve the desired result. We have one input text that is forwarded to the <code>cdkOption<\/code> input thanks to this syntax:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"351\" height=\"65\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-2.png\" alt=\"\" class=\"wp-image-1614\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-2.png 351w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-2-300x56.png 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Note that both inputs and outputs can be forwarded that way (<a href=\"https:\/\/angular.io\/guide\/directive-composition-api#including-inputs-and-outputs\" target=\"_blank\" rel=\"noopener\" title=\"\">code examples here<\/a>). The beauty of this approach is that our new directive has very little code while packing reusable features in a concise syntax. This is how we would use our new directive:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"209\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-3.png\" alt=\"\" class=\"wp-image-1615\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-3.png 579w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-3-300x108.png 300w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can find that <a href=\"https:\/\/stackblitz.com\/edit\/at-after-composition?file=src%2Fexample%2Fcdk-listbox-custom-typeahead-example.html,src%2Fexample%2Fcdk-listbox-custom-typeahead-example.ts,src%2Fexample%2FdraggableOption.directive.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">example live on Stackblitz here<\/a>. There are a few caveats with the directive composition API, mainly that it only works with <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/what-are-standalone-components\/\" target=\"_blank\" rel=\"noopener\" title=\"\">standalone directives<\/a> and that the options to &#8220;forward&#8221; inputs and outputs are limited for now. However, the composition API is an excellent option to make our code more reusable and immune to copying and pasting collections of directives from one component to another.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the past few weeks, we&#8217;ve covered different examples of directives, such as the CDK Listbox directive and the CDK Drag and Drop directive. I have mentioned that directives are underused in Angular applications these days, and one way to use more directives is to adopt the directive composition API. Let&#8217;s consider our previous example [&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,21,11],"tags":[],"class_list":["post-1611","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-directives"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1611"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1611\/revisions"}],"predecessor-version":[{"id":1617,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1611\/revisions\/1617"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}