Part1: Introduction to AngularJS


Recently I started learning AngularJS, it was very difficult for me to find some good detailed articles or beginner tutorials on AngularJS. I have to read from many different articles, books and tutorials. So I decided to put step by step help for beginners like me, so that they get complete required help from one single channel. Since AngularJS is a very rich framework so I decided to write in a series of post that will help beginners who want to learn or work on SPA(Single Page Application) using AngularJS. I am assuming that people reading this article are aware of html and javascript/jquery. So lets start with a short but detailed AngularJS tutorial where you will be able to quickly understand the power of AngularJS in the world of web.


You may have heard people talking about MVC/MVVM/MV*/ MVW (Model View Whatever) or have ever worked with, in some other programming languages. Though MV*/MVW is not a concept specific to AngularJS, but rather an approach to program architecture which is implemented in variety of programming languages and environments. It depends on the architect to architect that how he wants the architecture of the software and AngularJS provides facilty to design application in Model View and Whatever you want to have(lets say mvc,mvvm etc..).  There are bunch of topics to cover which we will be looking into one by one in the later part of tutorial, but still I will try to make you familiar with atleast some of the terminologies that I have used here in this part of tutorial.

So first lets try to understand MV* concept relative to AngularJS.


View in an application actually is a part which is rendered in a browser through which user can interact or see whatever data has been requested. In an AngularJS application view is composed of directives, filters and data-bindings. But to make view simple and maintainable we do not put all of our code into the View. This helps us to separate code from view and also makes it easy to write tests for the business logic.


Controller holds all of our application logic in AngularJS. The Controller controls and prepares the data into the form so that it can be rendered at the View. Functionally what controller actually does is, it collects all of data into the representational form and also takes from view and set into the Model after validating it. The controller is responsible for communicating the server code to fetch the data from a server using Ajax requests and send the data to back-end server from Views.

Model / View Model / $Scope

The most important and head part of the MV* architecture is Model or View Model or $Scope.  $Scope is a term which is introduced in AngularJS and we will discuss more on this in the later part of the article. Model is the bridge standing between Controllers and Views . There can be a controller which we can bind to two or more views. Lets suppose we have a controller assigned for a registration of users, for thispurpose you can have a different view for desktop and another view for mobile.

In reality the Controller is blank about views and has no information about the views and similarly View is independent of logic implemented or data present in the Controller. $scope acts as the communication tunnel between the Views and Controller.

Picture above is actually related to the sample application that we are going to create in the later part of the article. But for now from this picture atleast you can get an idea to MV* pattern.

What Exactly AngularJS Is?

In my terms AngularJS is nothing different to plain HTML but simply an extension to HTML with new attributes. AngularJS is a JavaScript MV* or MVW structured framework for dynamic web applications. It is maintained by Google to allow you to develop well architectured and easily maintainable web-applications. AngularJS makes use of declarative programming for building UI. AngularJS is client-sided so all these things are happening in browsers and you get the elegance of standalone application.

Why To Use AngularJS?

There are a lot many front-end frameworks available in the web world like Backbone, Knockout, Ember, Spline etc. and all of them have some pros and cons. But as the tutorial is all about AngularJS so the question arises that What is so peculiar about AngularJS ? So here is the answer to all your question.

With AngularJS you need to write lesser code as it allows you to reuse components. Also, it provides an easy way of two-way bindings and dependency injection (we will discuss more on next part of the tutorials). As AngularJS is client-sided so all these things are happening in browsers, which gives you feel of standalone applications(Desktop application).

Some of the reasons why I would prefer AngularJS or rather I say why AngularJS has become choice for the modern application architectures are described below:

1) You can create a template and reuse it in application multiple times.

2) You can bind data to any element in two ways, where two-way actually means changing data will automatically change element and changing element will change the data.

3) You can directly call the code-behind code in your html.

4) You can validate forms and input fields before submitting it without writing a single line of code.

5) Allows you to control complete dom structure show/hide, changing everything with AngularJS properties.

6) AngularJS allows you to write basic flow end-to-end testing, unit-testing, ui mocks.

In short, AngularJS provides all the features you need to build a CRUD application like data-binding, data validation, url routing, reusable HTML components and most importantly dependency injection.

Using the code

Lets start with a plain html which has nothing to do with AngularJS. First of all create a html page lets say Part1.html and add a Div with some ID.

  <title>AngularJS Tutorial</title>
    <div id='content'>

Now, move ahead and lets try to get familiar with AngularJS terminologies that I will be using in this part of the tutorial. Let us now modify our html page and add AngularJS reference to the html page which you can either download from here or use as a script reference on your head section of the page. In this sample application I have downloaded the file and referred from a physical location. Also we will add some directives more specific to AngularJS.

Directives are nothing but HTML tags and attributes (we will see more on this in the next part of the article). AngularJS provides numerous built-in directives like ng-app, ng-controller, ng-class, ng-repeat and many more. You can also create custom directives which we will learn in the next part of tutorial. Here is complete API reference available for AngularJS, you can search and get information about all directives or you can download Angular Cheat Sheet.

  <title>AngularJS Tutorial</title>
    <script src="angular.min.js"></script>
    <div id='content' ng-app='MyApp' ng-controller='MyController'>
      <h1> {{ article}}!</h1>

In the above code I have used some directives ng-app, ng-controller and a delimiter {{ }} so that AngularJS can evaluate the expression. Now the question comes in mind that what exaclty ng-app and ng-controller is and what importance does it have in an AngularJS application?

The ng-app is important of all directives in AngularJS. It actually defines the scope of your application that AngularJS can understand and it can be used either on the html tag itself or with the div tag if you want it to define its scope to the extent of a div. I wanted to scope it to the div section only, that is why I have set ng-app attribute to div. You can set it on the html tag if you want. Though it is not mandatory to provide a value to ng-app but you can put it blank only if you do not have any controller or model/view model/$scope. The sample application described here has a controller named “MyController” and I want to use data from controller so we need to provide the value for ng-app and here I have set it to “MyApp”.

Another attribute or I should say directive which is defined here is ng-controller. The ng-controller directive attaches a controller class to the view. It tells AngularJS that the controller for the DOM element has the name “MyController” and children DOM elements will have the same controller for them unless explicitly a new controller is specified for them using ng-controller.  In this application we have a controller named “MyController” which is attached to ng-app=”MyApp”.

And here comes the third thing {{ }} which we have never seen before in plain HTML or javascript/jQuery. {{ }} is AngularJS default delimiter for expression boundaries. It has to be used carefully in directives and HTML attributes and the reason is how AngularJS internally handles expressions.

Lets proceed with the remaining part of the sample application and see what has to be done next. I have created a file named “app.js” and it has below code:

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

You  can see here something angular.module. Module is like a container for different parts of your app like controllers, services, filters, directives, etc. Using modules you can divide your app into multiple parts which can work independently. Here we have assigned the module to a variable “app” which will contain our controller within it.

And now create another file named “mycontroller.js” which has below code:

app.controller("MyController", function($scope)
 $scope.article = "Hello there!!I am learning AngularJS";    

We have a new term here “$scope”. Scope holds the Model data that we need to pass to the view.  It uses AngularJS two-way data binding mechanism to bind model data to view.

So finally your html page should look like this.

  <title>AngularJS Tutorial</title>
    <script src="angular.min.js"></script>
    <script src="app.js" type="text/javascript"></script>
    <script src="mycontroller.js" type="text/javascript"></script>
    <div id='content' ng-app='MyApp' ng-controller='MyController'>
      <h1> {{ article }}!</h1>

When you open Part1.html in the browser.

Points of Interest

In this part of the tutorial we have seen an introductory part about AngularJS with a sample application. I hope this article would be helpful to the beginners who are interested to work on SPA.

In the next part of the tutorial we will learn Data Binding in AngularJS.

Sample Code

You can find sample code for the application demonstrated in the article from here:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.