{"id":2179,"date":"2017-07-22T22:52:40","date_gmt":"2017-07-22T22:52:40","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=2179"},"modified":"2018-10-10T17:15:56","modified_gmt":"2018-10-10T17:15:56","slug":"angularjs-controller-lifecycle-and-using-scope-lifecycle-hooks","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=2179","title":{"rendered":"AngularJS &ndash; Controller Lifecycle and using $scope lifecycle hooks"},"content":{"rendered":"<p><a href=\"https:\/\/stackoverflow.com\/questions\/16094940\/what-is-the-lifecycle-of-an-angularjs-controller\">This post here<\/a> answers a basic question I had as to what is the AngularJS controller lifecycle.<\/p>\n<p>The post links to a <a href=\"http:\/\/plnkr.co\/edit\/ZddQ5W?p=preview\">plnker<\/a> which demonstrates some of the lifecycle hooks \u2013 $viewContentLoaded and $destroy.<\/p>\n<p>These posts relate to the events used\/hooked in the example \u2013 but more digging is needed to expand on this area:<\/p>\n<p><a title=\"https:\/\/docs.angularjs.org\/api\/ngRoute\/directive\/ngView\" href=\"https:\/\/docs.angularjs.org\/api\/ngRoute\/directive\/ngView\">https:\/\/docs.angularjs.org\/api\/ngRoute\/directive\/ngView<\/a><\/p>\n<p><a title=\"https:\/\/stackoverflow.com\/questions\/28800426\/what-is-on-in-angularjs\" href=\"https:\/\/stackoverflow.com\/questions\/28800426\/what-is-on-in-angularjs\">https:\/\/stackoverflow.com\/questions\/28800426\/what-is-on-in-angularjs<\/a><\/p>\n<p><a title=\"https:\/\/docs.angularjs.org\/api\/ng\/type\/$rootScope.Scope\" href=\"https:\/\/docs.angularjs.org\/api\/ng\/type\/$rootScope.Scope\">https:\/\/docs.angularjs.org\/api\/ng\/type\/$rootScope.Scope<\/a><\/p>\n<p>Unfortunately my first attempt at trying this in an angularjs-up-and-running example failed with $scope being undefined.<\/p>\n<p>I initially used this code pattern to pass $scope into the controller:<\/p>\n<blockquote>\n<pre>angular.module('notesApp', [])<br \/><br \/>  .controller('SubCtrl', [function($scope) {<br \/><br \/>      console.log('SubCtrl has been constructed');<br \/>      $scope.$on('$viewContentLoaded', function() {<br \/>          console.log('SubCtrl has loaded');<br \/>      });<br \/>      $scope.$on('$destroy', function() {<br \/>          console.log('SubCtrl is no longer necessary');<br \/>      })<br \/><br \/>    var self = this;<br \/>    self.list = [<br \/>      {id: 1, label: 'Item 0'},<br \/>      {id: 2, label: 'Item 1'}<br \/>    ];<br \/><br \/>    self.add = function() {<br \/>      self.list.push({<br \/>        id: self.list.length + 1,<br \/>        label: 'Item ' + self.list.length<br \/>      });<br \/>    };<br \/>  }]);<br \/><\/pre>\n<\/blockquote>\n<p>The problem above is that the controller function is passed as an array (to avoid problems during minification) and so $scope has to be passed differently as per <a href=\"https:\/\/stackoverflow.com\/questions\/16284769\/angularjs-scope-undefined-when-controllers-are-inside-a-module\">this post here<\/a>.<\/p>\n<blockquote>\n<pre>angular.module('notesApp', [])<br \/>   .controller('SubCtrl', ['$scope', function(scope) {<br \/><br \/>      console.log('SubCtrl has been constructed');<br \/>      scope.$on('$viewContentLoaded', function() {<br \/>          console.log('SubCtrl has loaded');<br \/>      });<br \/>      scope.$on('$destroy', function() {<br \/>          console.log('SubCtrl is no longer necessary');<br \/>      })<br \/><br \/>    var self = this;<br \/>    self.list = [<br \/>      {id: 1, label: 'Item 0'},<br \/>      {id: 2, label: 'Item 1'}<br \/>    ];<br \/><br \/>    self.add = function() {<br \/>      self.list.push({<br \/>        id: self.list.length + 1,<br \/>        label: 'Item ' + self.list.length<br \/>      });<br \/>    };<br \/>  }]);<br \/><\/pre>\n<\/blockquote>\n<p>The complete working example follows:<\/p>\n<p><strong><u>app.js<\/u><\/strong><\/p>\n<blockquote>\n<pre>\/\/ File: chapter5\/need-for-service\/app.js<br \/>angular.module('notesApp', [])<br \/>  .controller('MainCtrl', [function() {<br \/>    var self = this;<br \/>    self.tab = 'first';<br \/>    self.open = function(tab) {<br \/>      self.tab = tab;<br \/>    };<br \/>  }])<br \/>  .controller('SubCtrl', ['$scope', function(scope) {<br \/><br \/>      console.log('SubCtrl has been constructed');<br \/>      scope.$on('$viewContentLoaded', function() {<br \/>          console.log('SubCtrl has loaded');<br \/>      });<br \/>      scope.$on('$destroy', function() {<br \/>          console.log('SubCtrl is no longer necessary');<br \/>      })<br \/><br \/>    var self = this;<br \/>    self.list = [<br \/>      {id: 1, label: 'Item 0'},<br \/>      {id: 2, label: 'Item 1'}<br \/>    ];<br \/><br \/>    self.add = function() {<br \/>      self.list.push({<br \/>        id: self.list.length + 1,<br \/>        label: 'Item ' + self.list.length<br \/>      });<br \/>    };<br \/>  }]);<br \/><\/pre>\n<\/blockquote>\n<p><strong><u>index.html<\/u><\/strong><\/p>\n<blockquote>\n<pre>&lt;!-- File: chapter5\/need-for-service\/index.html --&gt;<br \/>&lt;html ng-app=&quot;notesApp&quot;&gt;<br \/><br \/>&lt;head&gt;<br \/>  &lt;script<br \/>    src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.3.11\/angular.js&quot;&gt;<br \/>  &lt;\/script&gt;<br \/>  &lt;script src=&quot;app.js&quot;&gt;&lt;\/script&gt;<br \/>&lt;\/head&gt;<br \/><br \/>&lt;body ng-controller=&quot;MainCtrl as mainCtrl&quot;&gt;<br \/>  &lt;h1&gt;Hello Controllers!&lt;\/h1&gt;<br \/>  &lt;button ng-click=&quot;mainCtrl.open('first')&quot;&gt;Open First&lt;\/button&gt;<br \/>  &lt;button ng-click=&quot;mainCtrl.open('second')&quot;&gt;Open Second&lt;\/button&gt;<br \/>  &lt;div ng-switch on=&quot;mainCtrl.tab&quot;&gt;<br \/><br \/>    &lt;div ng-switch-when=&quot;first&quot;&gt;<br \/>      &lt;div ng-controller=&quot;SubCtrl as ctrl&quot;&gt;<br \/>        &lt;h3&gt;First tab&lt;\/h3&gt;<br \/>        &lt;ul&gt;<br \/>          &lt;li ng-repeat=&quot;item in ctrl.list&quot;&gt;<br \/>            &lt;span ng-bind=&quot;item.label&quot;&gt;&lt;\/span&gt;<br \/>          &lt;\/li&gt;<br \/>        &lt;\/ul&gt;<br \/><br \/>        &lt;button ng-click=&quot;ctrl.add()&quot;&gt;Add More Items&lt;\/button&gt;<br \/>      &lt;\/div&gt;<br \/><br \/>    &lt;\/div&gt;<br \/>    &lt;div ng-switch-when=&quot;second&quot;&gt;<br \/>      &lt;div ng-controller=&quot;SubCtrl as ctrl&quot;&gt;<br \/>        &lt;h3&gt;Second tab&lt;\/h3&gt;<br \/>        &lt;ul&gt;<br \/>          &lt;li ng-repeat=&quot;item in ctrl.list&quot;&gt;<br \/>            &lt;span ng-bind=&quot;item.label&quot;&gt;&lt;\/span&gt;<br \/>          &lt;\/li&gt;<br \/>        &lt;\/ul&gt;<br \/><br \/>        &lt;button ng-click=&quot;ctrl.add()&quot;&gt;Add More Items&lt;\/button&gt;<br \/>      &lt;\/div&gt;<br \/>    &lt;\/div&gt;<br \/>  &lt;\/div&gt;<br \/>&lt;\/body&gt;<br \/><br \/>&lt;\/html&gt;<br \/><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This post here answers a basic question I had as to what is the AngularJS controller lifecycle. The post links to a plnker which demonstrates some of the lifecycle hooks \u2013 $viewContentLoaded and $destroy. These posts relate to the events used\/hooked in the example \u2013 but more digging is needed to expand on this area: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[211],"tags":[16,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2179"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2179"}],"version-history":[{"count":2,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2179\/revisions"}],"predecessor-version":[{"id":2181,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2179\/revisions\/2181"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}