Learn AngularJS From Beginning - Part One

Actually, In this article, we will discuss about the basics of Angular js. Also we will discuss how to write down a very basic program in AngularJS.

Introduction to AngularJS

In 1990, Tim Berners Lee introduced a new markup language named HTML (Hyper Text Markup Language). The main objective of developing HTML was sharing information between different computers in a common readable format. At the beginning HTML is used to share static documents with hyperlinks and allow navigation between them. After that, in 1993, with the introduction of CGI (Common Gateway Interface), it was possible to display dynamic content generated by server side applications. At that time, Perl then followed by Java, PHP etc. were used for this purpose. But still interaction with complex applications through the browser was not enjoyable work and also sometimes, this task becomes to very hard to execute. But technology always kept moving forward. So at the same time, new versions of JavaScript, CSS and HTML were growing in popularity and also transforming the future of the web at a high level. AngularJS is a part of this new generation of libraries and frameworks that came to support the development of more productive, flexible and testable environments for web applications.

AngularJS was actually introduced in 2009 by Misko Hevery and Adam Abrons as an open source, client side javascript based framework that supports a high productivity web development experience. It was built on the belief that declarative programming is the best choice to construct the user interface, while imperative programming is much better and preferred to implement an application's business logic. To achieve this, AngularJS used traditional HTML by extending its current vocabulary, making the life of developers easier. In 2010, Miško Hevery was working at Google on a project called Feedback. For the large size of code in that project, Misko rewrote the project using his own client side framework and reduced the code size by nearly 10 times. Nowadays, the framework is used by more than 100 projects just at Google. The name of the framework was given by Adam Abrons, and it was inspired by the angle brackets of the HTML elements.

JavaScript Client Side Framework

JavaScript based client side application always runs in user’s device or PC and so, it shifts the workload to the user’s hardware and away from the server. At the same  time, like other server side based MVC frameworks like Struts, Spring MVC or ASP.NET do not switch the entire load from server machine to the client machine. Javascript based client side framework provides many significant advantages such as simplicity, rapid development, testability and also the ability to package the entire application and deploy it on all mobile devices and the web with related ease.

AngularJS makes the process even more faster and easier. Angular JS helps us to develop the frontend UI in a small time with proper support of unit testing for the client side code. Models and views in AngularJS are much simpler that what you find in most Javascript client side frameworks. Controllers, often missing in other javascript frameworks are likely most functional components in Angular JS. Once the AngularJS application is launched, the model, view, controller and all HTML documents are loaded on user’s device and completely run on the user’s hardware. As per the below diagram, we can see that calls are made to the backend REST services, where all business logic and business processes are loaded. The backend REST service can be located on any server either it is a private web server or a cloud server.

 

ng-app

Now we can write the same program with declaring the angular module just line below. Now the definition of angular module is as below –

  1. var myAppModule = angular.module('myApp', []);  

Write a Hello World in Angular JS

For doing this, we need to create one html file and another js file.
 
Code of App.js
  1. var testApp = angular.module('TestApp', []);  
Code of HelloWorld.html
  1. <!DOCTYPE html>  
  2. <html ng-app="TestApp">  
  3. <head>  
  4.     <title>Angular Js</title>  
  5.     <script src="angular.js"></script>  
  6.     <script src="app.js"></script>  
  7.     <script src="HelloWorld.js"></script>  
  8. </head>  
  9. <body>  
  10.     <div ng-controller="HelloController">  
  11.         <p>{{greeting.text}}, World</p>  
  12.     </div>  
  13. </body>  
  14. </html>  
Code of Helloworld.js 
  1. testApp.controller('HelloController'function ($scope) {  
  2.     $scope.greeting = { text: 'Hello' };  
  3.   
  4.   
  5. });  

Basically angular module acts as a main entry point of the angular applications. The pair bracket basically indicates the dependency injection concepts. This is one of the main advantages of AngularJS. Dependency injection (DI) is a design pattern where dependencies are defined in an application as part of the configuration. It helps us to avoid creating manual application dependencies. AngularJS uses dependency injection to load module dependencies when an application first starts. Actually dependency injection is not a new concept. It was around 10 years old concept and although it was consistently used in various frameworks.

Angular JS Models ($scope)

Many JavaScript client side frameworks also require you to create javascript model class. But it has not happened with AngularJS. AngularJS has a $scope objects that is used to store the application models. Scopes are attached to the DOM. The way to access the model is by using data properties to the $scope objects. The AngularJS $scope helps to simply Javascript Applications considerably. In AngularJS, $scope is used to gain access to the model related to a particular controller. $rootscope is a parent scope that can be used to save and access model properties that span multiple controllers. The use of $rootscope is highly discouraged in most designs, however. There is a only one $rootscope in an application. $scope is a child scope of $rootscope. A properly designed AngularJS application will have little or ideally no use of $rootscope to store model properties. Model properties can be added to the scope objects and once added they are available inside the view html templates.

Angular JS Controllers (ng-controllers)

In AngularJS, controllers act as a connection or bridge between views (i.e. HTML) and models (i.e. $scope). Controller is the area where we place all business logic specific for a particular view. When business logic is placed inside an application used by multiple controllers, it should be placed in AngularJS non-REST Services instead. Those services can be injected into any controller which needs to access that business logic with the help of dependency injection.

 

AngularJS controllers are the center of AngularJS applications and are probably the most important component to understand. AngularJS controllers have two main duties in an application. First, controllers should be used to initialize the model scope properties. When a controller is created and attached to the DOM, a child scope is created. The child scope holds a model used specifically for the controller to which it is attached. We can access the child scope by using $scope objects.

The second primary use of controllers is adding behavior of the $scope objects. We add behavior by adding method to the scope objects.

Presentation logic should not be placed inside the controllers but instead should be placed in the view. AngularJS has many features for DOM manipulation that help you avoid placing presentation logic in the controllers. The controller is also not the place where you should format data. AngularJS has features especially designed for formatting data, and that’s where data formatting should take place.

I will demonstrate the concept of controller objects in below example.
 
FirstProg.html
  1. <!DOCTYPE html>  
  2. <html ng-app="TestApp">  
  3. <head>  
  4.     <title>Angular Js -- First Prog</title>  
  5.     <script src="angular.js"></script>  
  6.     <script src="app.js"></script>  
  7.     <script src="FirstProg.js"></script>  
  8. </head>  
  9. <body>  
  10.     <div ng-controller="FirstProgController">  
  11.         <h1>Enter Your Name :</h1>  
  12.         <input type="text" width="200px" ng-model="name" />  
  13.         <h2>Your Name : {{name}}</h2>  
  14.     </div>  
  15. </body>  
  16. </html>  
FirstProg.Js
  1. testApp.controller('FirstProgController'function ($scope) {  
  2.     $scope.name = '';  
  3.   
  4.   
  5. });  

Nested Controller

In AngularJS, we can use the controllers in a nested concept. Since we need to attach the controller with the DOM, so we can attach multiple controllers at different levels of the DOM hierarchy. Since the controller directive creates a new child scope, so we get a hierarchy of scopes that inherit from each other. The $scope that each Controller receives will have access to properties and methods defined by Controllers higher up the hierarchy. In the below example, we create 3 nested controller directives which results to create four different scopes for the view -

  1.  The root scope
  2. The FirstController scope, which contains message and userName properties
  3. The SecondController scope, which inherits the message property but overrides (hides) the userName property from the previous
  4. The ThirdController scope, which overrides (hides) both the message property defined in FirstController and the userName property defined in SecondController.
In the below example, I will demonstrate this concept of nested controller.

Nestted.html
  1. <!DOCTYPE html>  
  2. <html ng-app="TestApp">  
  3. <head>  
  4.     <title>Angular Js -- First Prog</title>  
  5.     <script src="angular.js"></script>  
  6.     <script src="app.js"></script>  
  7.     <script src="nested.js"></script>  
  8. </head>  
  9. <body>  
  10.     <div ng-controller="FirstController">  
  11.         <p>Welcome {{username}}, {{message}}!</p>  
  12.         <hr />  
  13.         <div ng-controller="SecondController">  
  14.             <p>Welcome {{username}}, {{message}}!</p>  
  15.   
  16.             <hr />  
  17.             <div ng-controller="ThirdController">  
  18.                 <p>Welcome {{username}}, {{message}}!</p>  
  19.             </div>  
  20.         </div>  
  21.     </div>  
  22. </body>  
  23. </html>  
nested.js
  1. testApp.controller('FirstController'function ($scope) {  
  2.     $scope.username = 'Rabin';  
  3.     $scope.message = 'You logged in after 7 days.';  
  4. });  
  5.   
  6. testApp.controller('SecondController'function ($scope) {  
  7.     $scope.username = 'Scott';  
  8. });  
  9.   
  10. testApp.controller('ThirdController'function ($scope) {  
  11.     $scope.username = 'Thomash';  
  12.     $scope.message = 'This is your first time login..';  
  13. });  
ControllerAs Syntax

From AngularJS 1.1.5, angularjs introduced support for “Controller As” syntax. Using controller as makes it obvious which controller you 1are accessing in the template when multiple controllers apply to an element.

ControllerAs.html

  1. <!DOCTYPE html>  
  2. <html ng-app="TestApp">  
  3. <head>  
  4.     <title>Angular Js -- First Prog</title>  
  5.     <script src="angular.js"></script>  
  6.     <script src="app.js"></script>  
  7.     <script src="ControllerAs.js"></script>  
  8. </head>  
  9. <body>  
  10.     <div ng-controller="TestController as a">  
  11.         <h2>Hello {{a.username}}</h2>  
  12.         <h1>Enter Your Name :</h1>  
  13.         <input type="text" width="200px" ng-model="a.name" />  
  14.         <h2>Your Name : {{a.name}}</h2>  
  15.     </div>  
  16. </body>  
  17. </html>  
ControllerAs.js
  1. testApp.controller('TestController'function () {  
  2.     var self = this;  
  3.     self.name = '';  
  4.     self.username = 'Sujit';  
  5.   
  6. });  

Read more articles on AngularJS:


Similar Articles