We need to prepare our HTML file so that we can make use of AngularJS. Similar to the previous concepts.html
file, you will see that we have included the use of AngularJS via script. Open up your index.html
file in your favorite editor and you can start by adding the following code:
Now, to make sure that you are on the same page as I am, I want you to open this file in your favorite browser. You should see something like the following screenshot:
Got the previous code? It looks weird now due to the fact that we have not added the main JavaScript functionalities. We will be working on it in the next section.
Now, let me explain the code; notice that I have highlighted a few lines of it. These are the most important lines of the code that you should take note of in this example. The remaining lines are just the usual HTML code.
Adding in JavaScript with AngularJS
Now we will open our todo.js
file, which we created in the previous section. Open todo.js
in your favorite text editor. Let us begin by coding the following:
We are first going to define a controller, which we will be using for our app. Notice that we have named it todoCtrl
, which is mapped onto ng-controller
in the HTML file (index.html
), where ng-controller="todoCtrl"
means that todoCtrl
will be controlling this portion of the web page.
Also, notice the use of $scope
, which is an object that refers to the application model; it is the execution context for related expressions, such as ng-click
, ng-model
, and so on. Any such expressions of a predefined directive outside this scope will not be executed.
Let's start by initializing our to-do list. Within todoCtrl
, add the following code:
What $scope.todos
does is that it simply creates a list of objects, which contains the text, details, and whether this to-do is executed or not (true or false). Notice that todos
here is mapped to the collection todos
as seen in index.html
, where ng-repeat
is being used.
Let's move on by adding more functionalities. After $scope.currentDetails
, add the following three JavaScript functions:
The $scope.todoText
function resets todoText
after it has been pushed into the array. The $scope.addTodo
function does what it is suppose to do, simply adding a new to-do to the list of todos
as defined earlier. The beauty of AngularJS is that it uses standard JavaScript data structures that make manipulation so much easier.
The $scope.getRemaining
function simply calculates the number of items that are not done yet. Here, we can see two-way data-binding in action as this function executes whenever there is a change in the length of todos
.
The $scope.archive
function marks a to-do as done:true
in standard JavaScript manner.
By now, you should have noticed that all the JavaScript functions defined here are being used in index.html
under ng-controller="todoCtrl"
.
Let's now add three more JavaScript functions to complete the finishing touch for this sample application.
After the $scope.archive
function, add the following functions:
The $scope.showDetail
function simply retrieves the current to-do being clicked on and shows it on the div with ID #todoDetails
. The visibility of the #todoDetails
function is then set to visible
.
The $scope.close
function simply changes the visibility of #todoDetails
to hidden
.
Finally, $scope.addDetails
adds the details of the todo
item, and changes the visibility of #todoDetails
to hidden
once done.
Okay, so to see if we are on the same page, we now need to check our code. Save this file as todo.js
. Refresh your browser and you should still see the same user interface as per the previous screenshot.
Now, try clicking on the Detail button in front of work on chapter 2 examples, and you should see the following screenshot:
You will see the details of a particular to-do item. You can try to add some details for this item and click on Add Details. You can then click on other items and come back to this item later (without refreshing the browser), and you should still see the details in the text area.
You can also check off any of the items and you will see that the number of remaining to-do item decreases:
And of course, you can add new items by simply typing in the input box and clicking on the add button. You should notice that the number of items now increases: