{"id":1198,"date":"2015-03-18T07:48:15","date_gmt":"2015-03-18T07:48:15","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1198"},"modified":"2015-08-06T12:25:20","modified_gmt":"2015-08-06T12:25:20","slug":"angular-codeschool-notes","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/angular\/angular-codeschool-notes\/","title":{"rendered":"Angular CodeSchool Notes"},"content":{"rendered":"<p>Here are my notes from a free <a href=\"http:\/\/mbsy.co\/7Qm2h\">CodeSchool<\/a>\u00a0<a href=\"https:\/\/www.codeschool.com\/courses\/shaping-up-with-angular-js\">tutorial<\/a>\u00a0sponsored by Google:<\/p>\n<p><a href=\"http:\/\/ruoyusun.com\/2013\/05\/25\/things-i-wish-i-were-told-about-angular-js.html\">Things I wish I were told about Angular.js<\/a>.<\/p>\n<p><strong>Binding<\/strong><\/p>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;title&gt;AngularJS Tutorials&lt;\/title&gt;\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;div ng-app=\"\"&gt;\r\n\t\t\t&lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n\t\t\t&lt;h1&gt;{{data.message}}&lt;\/h1&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;script type=\"text\/javascript\" src=\"angular.js\"&gt;&lt;\/script&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Controllers<\/strong><\/p>\n<pre class=\"lang:default decode:true\">&lt;html&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;div ng-app=\"\"&gt;\r\n\t\t\t&lt;div ng-controller=\"FirstCtrl\"&gt;\r\n\t\t\t\t&lt;h1&gt;{{data.message + \" world\"}}&lt;\/h1&gt;\r\n\t\t\t\t&lt;div class=\"{{data.message}}\"&gt;\r\n\t\t\t\t\tMsg...\r\n\t\t\t\t&lt;\/div&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;script type=\"text\/javascript\" src=\"angular.js\"&gt;&lt;\/script&gt;\r\n\t\t&lt;script type=\"text\/javascript\" src=\"main.js\"&gt;&lt;\/script&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\/\/main.js\r\nfunction FirstCtrl($scope){\r\n$scope.data = {message: \"Hello\"};\r\n}<\/pre>\n<p><strong>Dot<\/strong><\/p>\n<p>https:\/\/www.youtube.com\/watch?v=DTx23w4z6Kc<\/p>\n<p class=\"ng-scope\" style=\"color: #333333;\">The initial setup:<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-app=\"\"&gt;\r\n  &lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n  &lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n\r\n  &lt;div ng-controller=\"FirstCtrl\"&gt;\r\n    &lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n    &lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n  &lt;\/div&gt;\r\n\r\n  &lt;div ng-controller=\"SecondCtrl\"&gt;\r\n    &lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n    &lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre class=\"lang:default decode:true\">function FirstCtrl($scope) {\r\n\r\n}\r\n\r\nfunction SecondCtrl($scope) {\r\n\r\n}<\/pre>\n<p class=\"ng-scope\" style=\"color: #333333;\"><strong>In this setup, all three instances of data.message are bound to each other through the first data.message, which is in the scope of the entire application.<\/strong><\/p>\n<p class=\"ng-scope\" style=\"color: #333333;\">If we were to replace the instances of data.message with just message,<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-app=\"\"&gt;\r\n  &lt;input type=\"text\" ng-model=\"message\"&gt;\r\n  &lt;h1&gt;{{ message }}&lt;\/h1&gt;\r\n\r\n  &lt;div ng-controller=\"FirstCtrl\"&gt;\r\n    &lt;input type=\"text\" ng-model=\"message\"&gt;\r\n    &lt;h1&gt;{{ message }}&lt;\/h1&gt;\r\n  &lt;\/div&gt;\r\n\r\n  &lt;div ng-controller=\"SecondCtrl\"&gt;\r\n    &lt;input type=\"text\" ng-model=\"message\"&gt;\r\n    &lt;h1&gt;{{ message }}&lt;\/h1&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p class=\"ng-scope\" style=\"color: #333333;\">It breaks the scope inheritance that was binding all the data.message instances. Now, each new ng-model of message is creating a new instance of message, and so each model will be an unbound instance.\u00a0<strong><em>So, important to note here is the data.message which will be available to all of the controllers!<\/em><\/strong><\/p>\n<p><strong>Sharing data between controllers<\/strong><\/p>\n<p>https:\/\/www.youtube.com\/watch?v=HXpHV5gWgyk<\/p>\n<p class=\"ng-scope\" style=\"color: #333333;\">The initial setup:<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-app=\"\"&gt;\r\n  &lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n  &lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n\r\n  &lt;div ng-controller=\"FirstCtrl\"&gt;\r\n    &lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n    &lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n  &lt;\/div&gt;\r\n\r\n  &lt;div ng-controller=\"SecondCtrl\"&gt;\r\n    &lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n    &lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre class=\"lang:default decode:true\">function FirstCtrl($scope) {\r\n\r\n}\r\n\r\nfunction SecondCtrl($scope) {\r\n\r\n}<\/pre>\n<p class=\"ng-scope\" style=\"color: #333333;\">With this setup, data.message is defined outside of either controller, scoped to the entire ng-app. The bindings within each of the controller divs both point to that \u2018parent\u2019 model. Because of this, all 3 instances of the data.message input are bound to each other because of the first instance, which is globally scoped in the application.<\/p>\n<p class=\"ng-scope\" style=\"color: #333333;\">If one were to remove the first model that does not reside in a controller, there is no parent model for either controller to bind to, so each instance of data.message will be scoped within its respective controller. Thus, neither data.message will be bound to the other.<\/p>\n<p class=\"ng-scope\" style=\"color: #333333;\">We\u2019d like to share the data model between the two controllers without creating a parent scope. This requires a service within a formally defined Angular application.<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-app=\"myApp\"&gt;\r\n\r\n  &lt;div ng-controller=\"FirstCtrl\"&gt;<\/pre>\n<pre class=\"lang:default decode:true\">var myApp = angular.module('myApp', []);\r\n\r\nmyApp.factory('Data', function () {\r\n  return { message: \"I'm data from a service\" };\r\n});\r\n\r\nfunction FirstCtrl($scope) {<\/pre>\n<p class=\"ng-scope\" style=\"color: #333333;\">This Data service can be injected into each of the controllers as a parameter. By doing this, we are now attaching the data.model to an app service, which repairs the binding between the two controller models.<\/p>\n<p class=\"ng-scope\" style=\"color: #333333;\"><a href=\"http:\/\/stackoverflow.com\/questions\/21919962\/angular-share-data-between-controllers\">Stackoverflow <\/a>thread on this.<\/p>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\t&lt;title&gt;&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body ng-app=\"myApp\"&gt;\r\n\t&lt;div ng-controller=\"FirstCtrl\"&gt;\r\n\t\t&lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n\t\t&lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n\t&lt;\/div&gt;\r\n\r\n\t&lt;div ng-controller=\"SecondCtrl\"&gt;\r\n\t\t&lt;input type=\"text\" ng-model=\"data.message\"&gt;\r\n\t\t&lt;h1&gt;{{ data.message }}&lt;\/h1&gt;\r\n\t&lt;\/div&gt;\r\n\t\r\n\t&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.3.14\/angular.min.js\"&gt;&lt;\/script&gt;\r\n\t&lt;script type=\"text\/javascript\" charset=\"utf-8\"&gt;\r\n\t\tangular.module('myApp', [])\r\n\r\n\t\t.factory('Data', function () {\r\n\t\t  return { message: \"I'm data from a service\" };\r\n\t\t})\r\n\r\n\t\t.controller('FirstCtrl', function ($scope, Data){\r\n\t\t\t$scope.data = Data;\r\n\t\t})\r\n\r\n\t\t.controller('SecondCtrl', function ($scope, Data){\r\n\t\t\t$scope.data = Data;\r\n\t\t});\r\n\t&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Method on a scope<\/strong><\/p>\n<pre class=\"lang:default decode:true\">function SecondCtrl($scope, Data) {\r\n  $scope.data = Data;\r\n\r\n  $scope.reversedMessage = function() {\r\n    return $scope.data.message.split(\"\").reverse().join(\"\");\r\n  };\r\n}<\/pre>\n<p><strong>Bootstraping<\/strong> <a href=\"https:\/\/docs.angularjs.org\/guide\/bootstrap\">https:\/\/docs.angularjs.org\/guide\/bootstrap<\/a><\/p>\n<p>Changing the data in the service and then showing it again, the ultimate example which eluded me for some time (<a href=\"http:\/\/plnkr.co\/edit\/EovKGN?p=preview\">Plunkr<\/a>):<\/p>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\t&lt;title&gt;&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body ng-app=\"myApp\"&gt;\r\n\t&lt;div ng-controller=\"FirstCtrl\"&gt;\r\n\t\t&lt;input type=\"text\" ng-model=\"data.msg\"&gt;\r\n\t\t&lt;h1&gt;{{ data.msg }}&lt;\/h1&gt;\r\n\r\n\t\t&lt;button type=\"\" ng-click=\"sayItBack()\"&gt;&lt;\/button&gt;\r\n\t&lt;\/div&gt;\r\n\r\n\t&lt;div ng-controller=\"SecondCtrl\"&gt;\r\n\t\t&lt;input type=\"text\" ng-model=\"data.msg\"&gt;\r\n\t\t&lt;h1&gt;{{ data.msg }}&lt;\/h1&gt;\r\n\t&lt;\/div&gt;\r\n\t\r\n\t&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.3.14\/angular.min.js\"&gt;&lt;\/script&gt;\r\n\t&lt;script type=\"text\/javascript\" charset=\"utf-8\"&gt;\r\n\t\tangular.module('myApp', [])\r\n\r\n\t\t.service('Data', function () {\r\n\t\t\tthis.data = {};\r\n\t\t\tthis.data.msg = \"Howdy!\";\r\n\r\n\t\t\tthis.getMsg = function() {\r\n\t\t\t    return this.data;\r\n\t\t\t};\r\n\r\n\t\t\tthis.setMsg = function(city){\r\n\t\t\t\tthis.data.msg = city;\r\n\t\t\t};\r\n\t\t})\r\n\r\n\t\t.controller('FirstCtrl', function ($scope, Data){\r\n\t\t\t$scope.data = Data.getMsg();\r\n\r\n\t\t\t$scope.sayItBack = function(){\r\n\t\t\t\tData.setMsg(\"Hello to you too!\");\r\n\t\t\t}\t\t\t\r\n\t\t})\r\n\r\n\t\t.controller('SecondCtrl', function ($scope, Data){\r\n\t\t\t$scope.data = Data.getMsg();\r\n\t\t});\r\n\t&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here are my notes from a free CodeSchool\u00a0tutorial\u00a0sponsored by Google: Things I wish I were told about Angular.js. Binding &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;AngularJS Tutorials&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;div&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1198","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1198"}],"version-history":[{"count":13,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1198\/revisions"}],"predecessor-version":[{"id":1662,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1198\/revisions\/1662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1319"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}