Coding Paradox

Blog On .Net, CSharp, Windows Phone Programming

Getting Started with AngularJS

without comments

One of the cool things i recently started learning was AngularJS. It is a JavaScript framework for building client side JavaScript applications which follows the Model-View-Controller pattern and ensures that your application is property architectured, easy to maintain and test. Some of the great features that it offers include two way data binding, client side templates, directives, routing, promises, dependency injection etc. All these features helps to reduce the complexity of the web application. If you have previous experience using Jquery, you will immediately notice how you can manipulate the DOM easily without writing any JavaScript code.

In this tutorial, we will start by developing a simple application using Angular which will highlight some of the key features of the framework.

You can create an Angular application by simple using a text editor and downloading Angular from the official site. However, in this example i will be using Visual Studio for the same. But it is not a necessity, you can simple use any text editor that you like.

Open Visual Studio 2013 and create a new ASP.Net application. From the templates, select the “Empty” option.

We will use Nuget to add AngularJS into our project. Right click the solution and select “Manage Nuget packages”. Search for “AngularJS” and from the result, select the AngularJS Core as shown below.

Add a new Html page to your project and call it Index.html. There are two basic requirements for using Angular in your application.

1) Add the following to your html page

This is the only file that you need to get all the core Angular features.

2) The “ng-app” directive.

Directives are one of the core components of Angular and they are used to extend the capabilities of HTML. The “ng-app” is the application directive and it is a special directive that Angular looks for in the application. if it finds it, Angular will initialize itself and start managing the page. Angular will only take care of the DOM sections where the directive is applied. We will add it to the <html> element so that it applies to the whole page

<html ng-app>

Lets add a simple binding expression to check whether Angular is working properly. Out html will look as shown below

The {{ 10 + 2 }} that we use is a Binding expression. Angular looks for these in our pages and evaluates the expression inside them. On running the page, you will see the following output.

If you remove the ng-app attribute, angular will not work and it will behave like a normal page.

Data Binding

To demonstrate simple data binding, lets add a simple input tag to our page as shown below

The ng-model=”name” directive creates a two way data binding between the value of the textbox and the “name” model variable within the scope of the application. Run the application and as you type something into the textbox, it will be immediately displayed in the page thanks to data binding.

Now that we have some basic directives and data binding, lets create something more meaningful. We will create a simple applications that will display the List of books user has read or is yet to read.

Add a new JavaScript file to the application and call it “controller.js”. Also add “Bootstrap” to the project via nuget.

Here is how our index.html code will look like

In our <div> we use the ng-controller directive to specify a controller. The controller controls the information that is displayed in a page. Controllers are functions that angular invokes. The ng-controller=”MainController” tells Angular to look for a MainController function that will manage the contents of the div. We can have multiple controllers inside our app and we can also nest controllers inside one another.

We define our MainController fucntion inside the Controller.js file. The code will look as shown below

The name of the function should be the same as that defined in the ng-controller attribute. That is how angular identifies which function to associate with the directive. In the function, we are defining a model that contains the name along with a list of books. Our controller function takes an argument “$scope” which is quite important. The “$” sign specifies that it is actually an Angular component. The “$scope” is what will act a glue between our view and the model. We can attach our model to the “$scope” and it will be available within the scope of the controller inside the view. All the functions and variables inside the controller will be included within the $scope.

The following code “{{readingList.Name}}’s Reading List” reads the Name property from the model and displays it using a binding expression.

In the below code

we use the “ng-repeat” directive which instructs Angular to repeat the <tr> for each book in the Books model. In the table, i have also used a checkbox whose model property is set to “book.isComplete”. Run the app and you will the see the output as shown below.

You can check/uncheck the checkbox and see how the value of isComplete changes dynamically thanks to two way data binding.

We can also change the way the data is displayed on the page using “Filters”. To demonstrate we will add a new Textbox to the page

We will modify the “ng-repeat” as shown below

This will tell Angular to apply a filter to the list which will be driven by what is typed in our textbox. Filters are added using the pipe “|” symbol. We can also make the title uppercase using a filter “{{book.Title | uppercase}}” filter. There are different types of filter available and you can also create your own filter. Run the application and as you type in the textbox, the result of the list will be filtered.

We can also attach functions to our buttons and other elements using the “ng-click” directive. We will add a textbox and a button to enable the user to add a book

Using the “ng-click” directive, we are binding the click event to call the “addBook()” function defined in the controller as shown below.

You can see we can get the value of the textbox using the “$scope.newTitle” as the model is part of the scope.

Run the application, type some title in the Textbox and click “Add”. The title that you added will be immediately added to the list.

I think this example gives you a fair idea of what can be done using Angular. In my next post, i will discuss and demonstrate some more features.

Download Solution

Written by Mako

July 7th, 2014 at 7:33 pm

Posted in AngularJS

Tagged with , , ,