{"id":890,"date":"2023-05-02T05:00:00","date_gmt":"2023-05-02T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=890"},"modified":"2026-02-25T01:45:44","modified_gmt":"2026-02-25T09:45:44","slug":"angular-16-preview-binding-router-information-to-component-inputs","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-16-preview-binding-router-information-to-component-inputs\/","title":{"rendered":"Binding Router Information to Component Inputs"},"content":{"rendered":"\n<p>Passing parameters to routes is a frequent task with Angular. Before Angular 16, we had to inject the <code><a href=\"https:\/\/angular.io\/api\/router\/ActivatedRoute\" target=\"_blank\" rel=\"noopener\" title=\"\">ActivatedRoute<\/a><\/code> service and retrieve the parameters from that service using the <code>snapshot<\/code> or <code>params<\/code> properties.<\/p>\n\n\n\n<p>Now, we can get those values automatically bound to the component inputs using the following router config:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"100\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image.png\" alt=\"\" class=\"wp-image-892\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image.png 643w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-300x47.png 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If you&#8217;re using the <code>RouterModule.forRoot()<\/code> syntax, then enabling component input binding looks like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">RouterModule.forRoot(routes, {<span class=\"hljs-attr\">bindToComponentInputs<\/span>: <span class=\"hljs-literal\">true<\/span>});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n\n\n\n<p>According to the <a href=\"https:\/\/angular.dev\/api\/router\/withComponentInputBinding\" target=\"_blank\" rel=\"noopener\" title=\"\">documentation<\/a>, this option can bind all route data with key-value pairs to component inputs: static or resolved route data, path parameters, matrix parameters, and query parameters.<\/p>\n\n\n\n<p>For instance, assuming we have a URL parameter called <code>id<\/code>, if we enable the component input binding feature, then all we need in our component is an <code>@Input<\/code> decorator to receive that value:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"67\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-1.png\" alt=\"\" class=\"wp-image-894\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>What I like about this option is that our components do not need the <code>ActivatedRoute<\/code> service anymore, meaning they don&#8217;t have to be used just as routed components. Instead, they could be used in other scenarios where the id is passed as an HTML attribute, just like any other non-routed component. As a result, our components are more generic and not tied to a single use case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Passing parameters to routes is a frequent task with Angular. Before Angular 16, we had to inject the ActivatedRoute service and retrieve the parameters from that service using the snapshot or params properties. Now, we can get those values automatically bound to the component inputs using the following router config: If you&#8217;re using the RouterModule.forRoot() [&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,8,25],"tags":[],"class_list":["post-890","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-routing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/890","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=890"}],"version-history":[{"count":6,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/890\/revisions"}],"predecessor-version":[{"id":2387,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/890\/revisions\/2387"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}