GETTING STARTED WITH ANGULARJS

Before we can do anything we need to create a simple HTML page that we can include AngularJS into. Create a file called index.html and use the following code:

Now that we have a HTML page to work with, head over to http://angularjs.org/ (opens a new tab) and copy the latest CDN link for AngularJS. Or just use this one: https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js. Now you have the CDN location of the latest AngularJS version, include it within the head tags of your HTML page as follows:

Okay, now we have a HTML page and AngularJS included ready for use, lets move on.

SETTING UP THE PAGE FOR ANGULARJS

There are a few things we need to do to tell AngularJS that it needs to render this page as an application. As this is a quick AngularJS tutorial we will not be going into detail as to how you can create multi-application and multi-controller pages, we are just going to declare this page as an AngularJS app, and its really simple!

Simply add ng-app=”MyTutorialApp” to the content DIV as follows:

Now we have told AngularJS that this DIV is an angular application, we need to declare which controller to use, as follows:

MainController is just a name that I made up, controllers should be named with some meaning and as this controller will be handling most of the application, main seems quite appropriate. Some would argue that this still is not enough definition, for this tutorial though, its fine! Okay, we now know what anything within the #content DIV will be controlled by the MainController, but where is this controller? We need to make it! Within the same directory as you index.html file we need to create a JS file called app.js and it should look like the following:

var app = angular.module('MyTutorialApp',[]);

All this file is doing is creating an AngularJS app called MyTutorialApp and assigning it to the app variable which we can use later on. Now we need to create another file called, maincontroller.js and it should look as follows:

app.controller("MainController", function($scope){ });

This is assigning a controller called, MainController to the MyTutorialApp application. All this JavaScript could be placed within the same JavaScript file but it is good practice to separate them out to keep your code maintainable and easier to understand. Just remember that app.js needs to be included before maincontroller.js. Lets do this now and include both files within out HTML so we can get started!

We now have the page and AngularJS application ready, lets move on with this AngularJS tutorial.

UNDERSTANDING THE SCOPE

When we created the maincontroller.js code you may have noticed the $scope variable that we added as a parameter to the controller function. This is where we will assign all our controller variables which will then make them available within the #content DIV in the HTML page. Take a look at the following example to understand this concept. Declare a $scope variable within the controller:

app.controller("MainController", function($scope){

$scope.understand = "I now understand how the scope works!";

});

In the above code we simply created a scope variable and assigned a string to it. Now we can access this within our HTML.

Within the above HTML we wrap the understand variable with {{}} so AngularJS knows it needs to do something with it. Note how we have not added the $scope. to the front of the variable name. If you now view your index.html file within a browser, you should see the following:

Now you should understand what scope variables are and that they are also available within the HTML by wrapping then with {{}}. You should also understand that the scope is available only to that controller so you could not access the knowledge variable from outside the #content DIV, unless the same controller was defined.

UNDERSTANDING BINDING

So, it was pretty cool that we can declare a scope variable within the JS and instantly access it within the HTML, but AngularJS can offer so much more.

Data binding is a large topic but this AngularJS tutorial will cover the most important parts to ensure you understand what is possible and allow you to investigate further on your own. To understand this concept, lets declare a scope variable which will act as a model. Modify maincontroller.js to look as below:

app.controller("MainController", function($scope){

$scope.inputValue = "";

});

Now lets add some additional HTML to our page:

Above we have added a simple text input and used ng-model to bind it to the scope variable inputValue. Note how we did not wrap it in {{}} as it is being used within a ng-* tag. Additionally we are outputting the inputValue scope variable onto the page as we did before with the understand variable.

By doing all this we have bound the input value to the variable. This means that whenever the input value changes, the variable will update to match this. Open the page in a web browser and start typing within the input box. You should see something like the following:

As you type the scope variable updates with the new input value and then as you are outputting the variable on this screen this also updates too, magic!

Now you have a way of keeping various elements and values in sync within the page. Lets move on to a more complex example to get a better understanding of what is possible. Update maincontroller.js to look like below:

We have declared three scope variables, selectedPerson is going to hold the object of the selected person. selectedGenre is going to hold the string value of the selected genre. Finally we have an array of objects which are people with their own musics tastes. What we are going to hope to achieve is have two select drop downs. One you will be able to select the person, then the other, thanks to binding will automatically update to allow you to select that persons music genres. Lets add this HTML elements to our page:

There are some new bits here so lets look at them in detail. Firstly we have a select input with ng-options tag. This allows us to populate this select input with the values from the people scope variable, using the name attribute within the person objects as the label, making the actual value of the select option, the object itself. This AngularJS tutorial is not going to go into great detail of ng-options you can read more about that here. Play around and see what is possible.

The next select input is populated a different way, for demonstrative purposes. The ng-repeat directive in my mind is one of the most powerful features of AngularJS. ng-repeat simply allows you to replicate HTML elements based on an array or set of objects. In the HTML above ng-repeat will create an option for each of the music genres within the people.music array based on the selectedPersons ID value which is determined by the first selected box as we have declared selectedPerson as its model as we did before with the input.