{"id":721,"date":"2023-03-30T05:00:00","date_gmt":"2023-03-30T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=721"},"modified":"2024-10-21T12:50:30","modified_gmt":"2024-10-21T19:50:30","slug":"ngoninit-vs-constructor-in-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/ngoninit-vs-constructor-in-angular\/","title":{"rendered":"ngOnInit vs. constructor in Angular"},"content":{"rendered":"\n<p>Today&#8217;s post is all about clarifying when to use a constructor and when to use <a href=\"https:\/\/angular.io\/api\/core\/OnInit\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>ngOnInit<\/code><\/a> to initialize data in our Angular components.<\/p>\n\n\n\n<p>From a chronological standpoint, the <strong>constructor is called first<\/strong>. Then, <strong>once all component inputs are initialized<\/strong>, Angular is going to call <code>ngOnInit<\/code>.<\/p>\n\n\n\n<p>From that definition, it makes sense to use ngOnInit only when your initialization code needs a <code><a href=\"https:\/\/angular.io\/api\/core\/Input\" target=\"_blank\" rel=\"noopener\" title=\"\">@Input<\/a><\/code> value to be set. Something along those lines:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"610\" height=\"225\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-40.png\" alt=\"\" class=\"wp-image-723\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-40.png 610w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-40-300x111.png 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s the only rule. Some might be surprised to read this, but you could initialize everything in your constructor if your component has no inputs. There is no performance impact in doing one or the other. <\/p>\n\n\n\n<p>I would argue that triggering an HTTP request in the constructor is better as it&#8217;s going to happen sooner (we&#8217;re talking milliseconds, though) than if we do it in <code>ngOnInit<\/code>.<\/p>\n\n\n\n<p>As a result, in the absence of any official recommendation in the <a href=\"https:\/\/angular.dev\/style-guide\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular style guide<\/a>, you can choose one or the other.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s post is all about clarifying when to use a constructor and when to use ngOnInit to initialize data in our Angular components. From a chronological standpoint, the constructor is called first. Then, once all component inputs are initialized, Angular is going to call ngOnInit. From that definition, it makes sense to use ngOnInit only [&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],"tags":[],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-angular","category-components"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/721","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=721"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":2209,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/721\/revisions\/2209"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}