{"id":2150,"date":"2018-07-19T19:33:37","date_gmt":"2018-07-20T00:33:37","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2150"},"modified":"2018-07-19T19:33:37","modified_gmt":"2018-07-20T00:33:37","slug":"angularjs-write-simple-app","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/19\/angularjs-write-simple-app\/","title":{"rendered":"AngularJS: How to write a simple To Do app"},"content":{"rendered":"<p>In this article I will describe how to create a simple &#8220;To Do&#8221; app in AngularJS and write about some of the basic concepts of AngularJS. The app I will be describing looks like this:<\/p>\n<p class='codepen'  data-height='520' data-theme-id='0' data-slug-hash='PaMMWQ' data-default-tab='js,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen AngularJS Simple To Do App  by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<h4><strong>Step 1: AngularJS CDN<\/strong><\/h4>\n<p>To create an AngularJS app, the first thing you will need is a CDN in the script portion of your HTML document.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.5.6\/angular.min.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>The two most important parts of this app are the HTML and the JavaScript. Here is what the HTML looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">  &lt;div ng-app=\"ToDo\" ng-controller=\"todoController\"&gt;\r\n      &lt;form name=\"todoform\" ng-submit=\"addTodo()\" class=\"form-style\"&gt;\r\n\r\n        &lt;input type=\"text\" name=\"newTodo\"\r\n                           ng-model=\"newTodo\"\r\n                           class=\"text-field\"\r\n                           placeholder=\"Type to do item here:\"&gt;\r\n        &lt;button ng-disabled=\"todoform.$invalid\" class\"btn-style\"&gt;Add&lt;\/button&gt;\r\n\r\n      &lt;\/form&gt;\r\n      &lt;br&gt;\r\n      &lt;h2&gt;To Do List:&lt;\/h2&gt;\r\n      &lt;div class=\"list-box\"&gt;\r\n        &lt;ul&gt;\r\n          &lt;li ng-repeat=\"todo in todos\"&gt;\r\n            &lt;input type=\"checkbox\" ng-model=\"todo.done\"\/&gt;\r\n            &lt;span ng-class=\"{'done': todo.done}\"&gt;{{todo.title}}&lt;\/span&gt;\r\n          &lt;\/li&gt;\r\n        &lt;\/ul&gt;\r\n      &lt;\/div&gt;\r\n      &lt;button ng-click=\"clearCompleted()\"&gt;Clear Completed&lt;\/button&gt;\r\n  &lt;\/div&gt;<\/pre>\n<p>And here is the JavaScript:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">  let app = angular.module('ToDo', []);\r\n  app.controller('todoController', ['$scope', function($scope) {\r\n    $scope.todos = [\r\n      {'title':'Sample List Item', 'done': false }\r\n    ];\r\n    $scope.addTodo = function(){\r\n      if($scope.newTodo){\r\n        $scope.todos.push({'title': $scope.newTodo, 'done': false})\r\n      }\r\n      $scope.newTodo = ''\r\n    }\r\n    $scope.clearCompleted = function() {\r\n      $scope.todos = $scope.todos.filter(function(item){\r\n        return !item.done\r\n\r\n      })\r\n    }\r\n  }])<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong>Step 2: Name the app in both HTML and JS<\/strong><\/h4>\n<p>In the HTML, we name the app with\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">ng-app=\"ToDo\"<\/code> like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div ng-app=\"ToDo\" ...<\/pre>\n<p>This corresponds to this line in the JavaScript:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let app = angular.module('ToDo', []);<\/pre>\n<p>The HTML tag is &#8220;ng-app&#8221;. &#8220;ng&#8221; stands for &#8220;Angular&#8221;. In the JavaScript, we set up an app variable and call the angular.module() method with two arguments. The first argument is the name of the app. In this case &#8220;ToDo&#8221;.<\/p>\n<p>For more information about ng-app, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/ng_ng-app.asp\">https:\/\/www.w3schools.com\/angular\/ng_ng-app.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngApp\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngApp<\/a><\/p>\n<p>For more information about angular.module(), see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/angular_modules.asp\">https:\/\/www.w3schools.com\/angular\/angular_modules.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/function\/angular.module\">https:\/\/docs.angularjs.org\/api\/ng\/function\/angular.module<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Step 3: Set up the controller<\/strong><\/h4>\n<p>In the HTML, the controller is set up with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">ng-controller=\"todoController\"<\/code> like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div ... ng-controller=\"todoController\"&gt;<\/pre>\n<p>This corresponds to this line in the JavaScript:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">app.controller('todoController', ['$scope', function($scope) {<\/pre>\n<p>The HTML tag is &#8220;ng-controller&#8221;. In the JavaScript, we use the app variable and call the controller() method with two arguments. The first argument is the name of the controller that was defined in the HTML. In this case &#8220;todoController&#8221;. The second argument is an array, with the first argument being &#8220;$scope&#8221; and the second being the body of the &#8220;todoController&#8221; function.<\/p>\n<p>For more information about ng-controller, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/angular_controllers.asp\">https:\/\/www.w3schools.com\/angular\/angular_controllers.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngController\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngController<\/a><\/p>\n<p>Here is what w3schools says about angular controllers:<\/p>\n<blockquote><p>The ng-controller=&#8221;myCtrl&#8221; attribute is an AngularJS directive. It defines a controller.<\/p>\n<p>The myCtrl function is a JavaScript function.<\/p>\n<p>AngularJS will invoke the controller with a $scope object.<\/p>\n<p>In AngularJS, $scope is the application object (the owner of application variables and functions).<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h4><strong>Step 4: Specify a function to run when the form is submitted<br \/>\n<\/strong><\/h4>\n<p>In the HTML we set up a form with an &#8220;ng-submit&#8221; tag: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">ng-submit=\"addTodo()\"<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;form name=\"todoform\" ng-submit=\"addTodo()\" class=\"form-style\"&gt;<\/pre>\n<p>This specifies what controller function to run when the form is submitted. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">\"addTodo()\"<\/code> corresponds to the $scope.addTodo function definition in the controller portion of the Angular code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$scope.addTodo = function(){\r\n if($scope.newTodo){\r\n  $scope.todos.push({'title': $scope.newTodo, 'done': false})\r\n }\r\n  $scope.newTodo = ''\r\n}\r\n<\/pre>\n<p>For more information about ng-submit, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/ng_ng-submit.asp\">https:\/\/www.w3schools.com\/angular\/ng_ng-submit.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngSubmit\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngSubmit<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Step 5: Bind the form input to the angular controller using ng-model<br \/>\n<\/strong><\/h4>\n<p>Here is what w3schools says about Angular&#8217;s ng-model directive:<\/p>\n<blockquote><p>The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.<\/p><\/blockquote>\n<p>In the HTML form, the data &#8220;newTodo&#8221; is defined in ng-model. This is the text input typed by the user in the text field. This is essentially a pipeline from the web page to the Angular controller.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;input type=\"text\" name=\"newTodo\" ng-model=\"newTodo\" class=\"text-field\" placeholder=\"Type to do item here:\"&gt;<\/pre>\n<p>The Angular controller defines what happens to this data when the addTo function is called:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$scope.addTodo = function(){\r\n  if($scope.newTodo){\r\n     $scope.todos.push({'title': $scope.newTodo, 'done': false})\r\n  }\r\n  $scope.newTodo = ''\r\n}<\/pre>\n<p>If $scope.newTodo is present, (meaning something was typed into the text field before the user clicked the &#8220;Add&#8221; button), then assign the text as a value to the &#8216;title&#8217; key in a small object and push that object into an array called &#8216;todos&#8217;.\u00a0 This array of objects is also defined in the controller:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$scope.todos = [\r\n  {'title':'Sample List Item', 'done': false }\r\n];<\/pre>\n<p>For more information about ng-model, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/angular_model.asp\">https:\/\/www.w3schools.com\/angular\/angular_model.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngModel\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngModel<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Step 6: Display the output in the web app<\/strong><\/h4>\n<p>When a &#8216;newTodo&#8217; item is added to the list from Step 5 above, it is displayed in the HTML immediately because of this HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;ul&gt;\r\n  &lt;li ng-repeat=\"todo in todos\"&gt;\r\n\t&lt;input type=\"checkbox\" ng-model=\"todo.done\"\/&gt;\r\n\t&lt;span ng-class=\"{'done': todo.done}\"&gt;{{todo.title}}&lt;\/span&gt;\r\n  &lt;\/li&gt;\r\n&lt;\/ul&gt;<\/pre>\n<p>There are two parts involved in displaying the output in the web page:<\/p>\n<p>1. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">ng-repeat=\"todo in todos\"<\/code><br \/>\n2. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">{{todo.title}}<\/code><\/p>\n<p>The Angular ng-repeat directive will basically repeat the HTML element it is defined in for as many data elements as are found in the container that it specifies. In this case the container (usually an array) is called &#8216;todos&#8217;. This is like a for loop. For example:<\/p>\n<p>For todo in todos, repeat this &lt;li&gt; element and display the title of each todo.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">{{todo.title}}<\/code> is what displays the value associated with the key &#8216;title&#8217; in each todo object.<\/p>\n<p>For more information about ng-repeat, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/ng_ng-repeat.asp\">https:\/\/www.w3schools.com\/angular\/ng_ng-repeat.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngRepeat\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngRepeat<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Step 7: Handle checkboxes for deleting items<br \/>\n<\/strong><\/h4>\n<p>In addition to outputting the title of each todo in the previous step, we can also output a checkbox with each todo which will allow that todo item to be deleted. To do this, we will use ng-model again, but this time, bind to a different key in the todo object. For example, in the HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;input type=\"checkbox\" ng-model=\"todo.done\"\/&gt;\r\n&lt;span ng-class=\"{'done': todo.done}\"&gt;{{todo.title}}&lt;\/span&gt;<\/pre>\n<p>This time ng-model is binding to &#8220;todo.done&#8221;, which by default is set to false. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">{'title': $scope.newTodo, 'done': false}<\/pre>\n<p>The next line, which is a span, uses <code id=\"actual-html-code\" class=\" cm-s-default\" data-lang=\"xml\" data-og-lang=\"xml\" data-alt-lang=\"xml\"><span class=\"cm-attribute\">ng-class<\/span><\/code> which tells the CSS how to style the todo item if the checkbox is clicked. ng-class basically says &#8220;If this condition is true, then use the CSS styling associated with this CSS class. Here is the CSS for the &#8220;done&#8221; class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.done {\r\n  text-decoration: line-through;\r\n  color: #ccc;\r\n}<\/pre>\n<p>When a checkbox is clicked, the CSS &#8216;done&#8217; class is activated due to the use of ng-class which gives a strike through style to the item. Also, because of the data binding with ng-model, the todo.done value is toggled to true.<\/p>\n<p>For more information about ng-class, see:<br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngClass\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngClass<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Step 8: Removing checked items<\/strong><\/h4>\n<p>The final step is to set up a button that will remove all completed todo items that have already been checked. Unlike the ng-submit button we used earlier, this time we will use ng-click with the argument being the name of the angular function that is defined in the controller.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;button ng-click=\"clearCompleted()\"&gt;Clear Completed&lt;\/button&gt;<\/pre>\n<p>The ng-click directive tells Angular to launch the clearCompleted() function when the &#8220;Clear Completed&#8221; button is clicked. The clearCompleted() function defined in the controller looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$scope.clearCompleted = function() {\r\n    $scope.todos = $scope.todos.filter(function(item){\r\n    return !item.done\r\n})<\/pre>\n<p>Which basically means, use a filter to only return todo items that don&#8217;t have todo.done = true.<\/p>\n<p>For more information about the ng-click directive, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/angular\/ng_ng-click.asp\">https:\/\/www.w3schools.com\/angular\/ng_ng-click.asp<\/a><br \/>\n<a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngClick\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngClick<\/a><\/p>\n<p>Well, that&#8217;s it for this introduction to Angular.<\/p>\n<p>Note: This To Do app does not save list state, so refreshing the page will delete everything on the list. I will write about how to save the list so that it persists in another article.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will describe how to create a simple &#8220;To Do&#8221; app in AngularJS and write about some of the basic concepts of AngularJS. The app I will be describing looks like this: &nbsp; Step 1: AngularJS CDN To create an AngularJS app, the first thing you will need is a CDN in &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/19\/angularjs-write-simple-app\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">AngularJS: How to write a simple To Do app<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[148,44],"tags":[156,149,10,105,45,155,150,157,154,151,152,153],"class_list":["post-2150","post","type-post","status-publish","format-standard","hentry","category-angular","category-javascript-language","tag-angular-module","tag-angularjs","tag-css","tag-html","tag-javascript","tag-ng-app","tag-ng-class","tag-ng-click","tag-ng-controller","tag-ng-model","tag-ng-repeat","tag-ng-submit"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2150"}],"version-history":[{"count":30,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2150\/revisions"}],"predecessor-version":[{"id":2184,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2150\/revisions\/2184"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}