{"id":2240,"date":"2024-12-12T10:43:07","date_gmt":"2024-12-12T18:43:07","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2240"},"modified":"2024-12-12T10:43:07","modified_gmt":"2024-12-12T18:43:07","slug":"running-initialization-code-when-angular-loads-router-data-updates","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/running-initialization-code-when-angular-loads-router-data-updates\/","title":{"rendered":"Running initialization code when Angular loads + router data updates"},"content":{"rendered":"\n<p> I\u2019m posting a few essential articles to revisit, updates to know about, and one question to ponder this week:<\/p>\n\n\n\n<p><strong>Three articles to revisit and associated updates with Angular 19<\/strong><\/p>\n\n\n\n<p>When we need to initialize an application with server data (environment variables, for instance), we can use Angular&#8217;s <code>APP_INITIALIZER<\/code>. For more information, revisit this <a href=\"https:\/\/blog.angulartraining.com\/how-to-run-initialization-tasks-before-loading-an-angular-application-2555a0fd0d82\" target=\"_blank\" rel=\"noopener\" title=\"\">tutorial about APP_INITIALIZER<\/a>.<\/p>\n\n\n\n<p>Angular 19 introduced a new syntax for this feature. This function must be called in the array of providers of your app bootstrap as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"508\" height=\"254\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image.png\" alt=\"\" class=\"wp-image-2241\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image.png 508w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image-300x150.png 300w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can return an <code>Observable<\/code> or a <code>Promise<\/code> from your init function. The <a href=\"https:\/\/angular.dev\/api\/core\/provideAppInitializer?tab=api\" target=\"_blank\" rel=\"noopener\" title=\"\">official documentation is here<\/a>.<\/p>\n\n\n\n<p>Another interesting feature expanded in recent versions is the ability to pass data to components using the router. I wrote about <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-use-angular-router-state\/\" target=\"_blank\" rel=\"noopener\" title=\"\">router state<\/a> and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/resolver-function-for-the-angular-router\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to use resolvers<\/a> in the past, and even <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-16-preview-binding-router-information-to-component-inputs\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to map router data to component inputs<\/a>.<\/p>\n\n\n\n<p>With Angular 19, we can pass data to a <code>RouterOutlet<\/code> to share information between parent and child components as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"987\" height=\"301\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image-1.png\" alt=\"\" class=\"wp-image-2242\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image-1.png 987w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image-1-300x91.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/12\/image-1-768x234.png 768w\" sizes=\"auto, (max-width: 987px) 100vw, 987px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>One question to ponder this week:<\/strong><\/p>\n\n\n\n<p>How do you pass data to your routed components? Do you use any of the features described above or just inject services in the routed component? Is there anything you could improve in your architecture using these tools?<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m posting a few essential articles to revisit, updates to know about, and one question to ponder this week: Three articles to revisit and associated updates with Angular 19 When we need to initialize an application with server data (environment variables, for instance), we can use Angular&#8217;s APP_INITIALIZER. For more information, revisit this tutorial about [&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,25],"tags":[],"class_list":["post-2240","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-routing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2240","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=2240"}],"version-history":[{"count":1,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2240\/revisions"}],"predecessor-version":[{"id":2243,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2240\/revisions\/2243"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}