{"id":1337,"date":"2023-08-17T05:00:00","date_gmt":"2023-08-17T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1337"},"modified":"2025-04-01T11:53:02","modified_gmt":"2025-04-01T18:53:02","slug":"ngonchanges-lifecycle-hook","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/ngonchanges-lifecycle-hook\/","title":{"rendered":"ngOnChanges lifecycle hook"},"content":{"rendered":"\n<p><code><a href=\"https:\/\/angular.dev\/api\/core\/OnChanges\" target=\"_blank\" rel=\"noopener\" title=\"\">ngOnChanges<\/a><\/code> is an Angular lifecycle hook that is called whenever one or more of the bound input properties of a component changes. This hook helps update the component&#8217;s state or view in response to changes to the input properties.<\/p>\n\n\n\n<p>To use it, implement the <code>OnChanges<\/code> interface as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"472\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-21.png\" alt=\"\" class=\"wp-image-1338\" style=\"width:590px;height:362px\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-21.png 770w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-21-300x184.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-21-768x471.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above <code>ngOnChanges<\/code> method will run every time the parent component updates the value of <code>name<\/code>. We can get more information about what changed using the parameter of that method, an object that maps each changed property name to an <code>SimpleChange<\/code> object. The <code><a href=\"https:\/\/angular.dev\/api\/core\/SimpleChange\" target=\"_blank\" rel=\"noopener\" title=\"\">SimpleChange<\/a><\/code> object contains the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>previousValue<\/code>: The value of the property before it changed.<\/li>\n\n\n\n<li><code>currentValue<\/code>: The value of the property after it changed.<\/li>\n\n\n\n<li><code>firstChange<\/code>: boolean value, <code>true<\/code> if it&#8217;s the first change made to that input, <code>false<\/code> otherwise.<\/li>\n<\/ul>\n\n\n\n<p>As an example, the above code would output that object to the console as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"318\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-22.png\" alt=\"\" class=\"wp-image-1339\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-22.png 450w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-22-300x212.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can find that example in action <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-r4b3zx?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fhello%2Fhello.component.html,src%2Fhello%2Fhello.component.ts,src%2Fmain.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">on Stackblitz<\/a>. The most common real-life example of using ngOnChanges is to be notified when an object <code>ID<\/code> changes so the component can request data for that <code>ID<\/code> using a service that makes an HTTP request, for instance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ngOnChanges is an Angular lifecycle hook that is called whenever one or more of the bound input properties of a component changes. This hook helps update the component&#8217;s state or view in response to changes to the input properties. To use it, implement the OnChanges interface as follows: The above ngOnChanges method will run every [&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-1337","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\/1337","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=1337"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1337\/revisions"}],"predecessor-version":[{"id":2283,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1337\/revisions\/2283"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}