{"id":4886,"date":"2017-05-07T10:13:54","date_gmt":"2017-05-07T10:13:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4886"},"modified":"2017-05-07T10:13:54","modified_gmt":"2017-05-07T10:13:54","slug":"lifecycle-hooks-in-angular-2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/lifecycle-hooks-in-angular-2\/","title":{"rendered":"Lifecycle hooks in Angular 2"},"content":{"rendered":"<p>If you haven&#8217;t already read this post <a href=\"https:\/\/angular.io\/docs\/ts\/latest\/guide\/lifecycle-hooks.html\" target=\"_blank\">LIFECYCLE HOOKS<\/a>, I would highly recommend you go and read that first.<\/p>\n<p>This is a really short post on just getting up and running with lifecycle hooks.<\/p>\n<p><strong>What are lifecycle hooks?<\/strong><\/p>\n<p>Angular 2 offers ways for our class\/components to be called when certain key parts of a lifecycle workflow occur. The most obvious would be after creation, some form of initialization phase and ofcourse conversely when a class\/component is cleaned up and potentially disposed of.<\/p>\n<p>These are not the only lifecycle hooks, but this post is not mean&#8217;t to replicate everything in Angular 2&#8217;s documentation, but instead highlight how we use the hooks in our code. <\/p>\n<p><strong>How do we use a lifecycle hook<\/strong><\/p>\n<p>Let&#8217;s look at implementing a component with the two (probabaly) most used hooks, OnInit and OnDestroy, as these are obviously especially useful in situations where, maybe state needs to be loaded and stored.<\/p>\n<p>As usual, we need to import the two interfaces, hence we need the line<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { OnInit, OnDestroy } from '@angular\/core';\r\n<\/pre>\n<p>OnInit and OnDestroy look like this<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nexport interface OnInit {\r\n   ngOnInit() : void;\r\n}\r\n\r\nexport interface OnDestroy {\r\n   ngOnDestroy(): void;\r\n}\r\n<\/pre>\n<p>Our component would then implement these interfaces, thus<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n@Component({\r\n})\r\nexport class DetailsComponent \r\n      implements OnInit, OnDestroy {\r\n\r\n   ngOnInit(): void {\r\n   }\r\n\r\n   ngOnDestroy(): void {\r\n   }\r\n}\r\n<\/pre>\n<p>and that&#8217;s all there is to it. <\/p>\n<p><em>Note: In the above I said we <strong>need<\/strong> to import the interfaces. In reality this is not true, interfaces are optional (as they&#8217;re really a TypeScript construct to aid in type checking etc.). What Angular 2 is really looking for is the specially named methods, i.e. ngOnInit and ngOnDestroy in our case.<\/em><\/p>\n<p>See <a href=\"https:\/\/angular.io\/docs\/ts\/latest\/guide\/lifecycle-hooks.html\" target=\"_blank\">LIFECYCLE HOOKS<\/a> or more specifically the section on Lifecycle sequence for an understand when different parts of the life cycle hooks get called.<\/p>\n<p>For completeness, I&#8217;ll list the same here, but without all the descriptions.<\/p>\n<ul>\n<li>ngOnChanges<\/li>\n<li>ngOnInit<\/li>\n<li>ngDoCheck<\/li>\n<li>ngAfterContentInit<\/li>\n<li>ngAfterContentChecked<\/li>\n<li>ngAfterViewInit<\/li>\n<li>ngAfterViewChecked<\/li>\n<li>ngOnDestroy<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you haven&#8217;t already read this post LIFECYCLE HOOKS, I would highly recommend you go and read that first. This is a really short post on just getting up and running with lifecycle hooks. What are lifecycle hooks? Angular 2 offers ways for our class\/components to be called when certain key parts of a lifecycle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[170],"tags":[],"class_list":["post-4886","post","type-post","status-publish","format-standard","hentry","category-angular"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4886"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4886\/revisions"}],"predecessor-version":[{"id":4905,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4886\/revisions\/4905"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}