About

Angular JS is the one of the finest JavaScript frameworks to create dynamic web applications by writing less code with an easy syntax in HTML files.

RoadMap

In this article you will get an idea of the following concepts.

Introduction Prerequisites Download Angular Js Angular Js Editors Angular Js Architecture About ng-app and Binding Expressions ng-controller ng-model ng-show and ng-hide

Introduction

Angular JS is the one of the best frontend frameworks for implementing any kind of dynamic web application using JavaScript and HTML for the following reasons.

It helps you to organize your JavaScript code by creating controllers, modules and so on. The website creation is very fast, as fast as adding attributes to the controls. It is compatible with jQuery as well. It's suitable for testing the application since the way of writing JavaScript is organized.

Angular JS started as a Google project but now it is an open-source project. So it's available in GitHub and anyone can contribute to it.

The official site for Angular JS.

Until now, there are many applications developed using Angular JS. You can get a list of all those applications from the URL BuiltWith AngularJS.

In Angular JS web applications, using Directives you can tell HTML to trigger your JavaScript functions.

Angular JS provides many directives, even though most of the directives are understandable by the name itself. The directives start with “ng-”. Here ng refers to and is pronounced as Angular.

Angular JS has no other dependencies. So you just need to include the JavaScript file whereever required.

Prerequisites

To implement web applications using the frontend application framework, you just need to have some idea of the following concepts:

HTML CSS JavaScript

Download AngularJS

The Angular js files you can download from AngularJS. You can even use the CDN URL to access the JavaScript file.

The CDN URL is: https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js

AngularJS Editors

There are many suitable editors available (online/offline) to implement or practice Angular JS applications. They are the following:

Plunker. WebStorm. JS Fiddler (JSFiddle ) and so on.

AngularJS Architecture

AngualrJS should be categorized as pinny needed MV* framework and it also supports Unit tests and both integrated end to end tests.

Model: the Model is used to restore the data and state of your application.

View: Where you actually render to the user the information they want.

It stands like any other Controller / Presenter / ViewModel and so on. Even though in most of the cases we will use the controller.

So, Controllers are the main components in Angular applications, Angular JS supports two-way binding. That means that whenever the user enters into the form fields they are instantly updated in Angular models.

So, let's start a sample application using Angular JS to get a brief idea of it.

Note: in these examples, I am using Angular Js 1.3.8, the latest version, since this version contains many features and at the same time, some of the features that are supported in 1.2.x would not be supported in the 1.3.8 version (for example, Global controller functions are not supported in 1.3.x by default).

ng-app and Binding Expressions

In this example, you will implement a basic application using Angular JS.

<!DOCTYPE html > < html > < head > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js" > </ script > </ head > < body > < h1 > Welcome to Angular Application </ h1 > < div id = "AngularDiv" ng-app > This div is controlled by Angular JS: {{ 1 + 2 }} < br /> <!--4. {{ }} Data binding Expressions with in a single quote treated as constant string -- > To Display Constants / Static ones in Angular JS: {{ '1 + 2' }} </ div > < div id = "NormalDiv" > Normal div doesn't handled by Angular JS: {{ 1 + 2 }} </ div > </ body > </ html >



The previously described HTML file contains five notified points as specified in the comments section of HTML.

The Angular JS script CDN reference added at the head of HTML file.

ng-app: The directive ng-app is added as an attribute to one of the div with an Id of AngularDiv. It is a special directive since whenever it is found inside a HTML page it starts the action of managing the page.

{{ }}: This format or notation is called Binding Expressions in Angular JS.

So, whenever you have defined some expression inside the Binding Expressions ({{ }}) that is controlled by AngularJS directive (ng-app) automatically it evaluates and displays the results in an HTML page.





If you want to display an expression within binding expressions ( {{ }} ) without evaluation then you need to enclose it with single quotes as described in the 4th point of the HTML file.

The other div with the Id NormalDiv does not have an assigned attribute (ng-app). So it displays data normally without evaluating the data within the binding expressions.

Now, you can save the HTML file and run it in a browser and the output will be displayed as follows.

You can run the sample application using the Plnkr tool at AngularJs Starter

Until now, you got an idea of the special directive named “ng-app” and how you can use the binding expressions to evaluate the data. Now, you can move to the next step of writing JavaScript code and trigger that code in the HTML file using Angular JS directives.

ng-controller

The primary responsibility of the controller is to create the scope object and that scope can communicate with the view in both directions. You can bind the properties and functions to the scope object.

<!DOCTYPE html > < html > < head > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js" > </ script > < script src = "Demo.js" > </ script > </ head > < body ng-app = 'DemoApp' > < h1 ng-controller = 'DemoController' > {{ angularjs }} </ h1 > </ body > </ html >



The HTML file contains mainly the following 3 points:

Angular Js script file CDN path reference.



Demo.js file reference containing a few lines of JavaScript code.



ng-app refers to the module name (DemoApp), it refers to any controllers created under it that is part of that module. Ng-controller refers to the controller name (DemoController).

The Demo.js file contains the following lines of JavaScript code.

var WelcomeAngular = function ($scope) { $scope.angularjs = "Welcome to Angular Js" ; }; var app = angular.module( "DemoApp" , []) app.controller( 'DemoController' , WelcomeAngular);



The first step of JavaScript file contains one variable named WelcomeAngular. It refers to one function with the scope object. The scope object contains one property named angualrjs.

In the second step you are creating an Angular module named “DemoApp” to the variable “app” and then assigning a controller named “DemoController” to that app.

You can now run the HTML file and then you will see the output as “Welcome to Angular Js” that was specified in the JavaScript file to the property named angularjs in the scope object.

You can run the sample application using the Plnkr tool at AngularJs Controller

ng-model

ng-model is also one of the directives, it can bind to the input, select or custom form controls using the property named ng-model.

<!DOCTYPE html > < html > < head > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js" > </ script > </ head > < body ng-app > < h1 > Employee Search </ h1 > < form > < label > Employee Name: </ label > < input type = "text" placeholder = "Employee name to find" ng-model = "empname" /> < br /> < pre > You entered employee name is: {{empname}} </ pre > </ form > </ body > </ html >

Added the Angular JS CDN path. ng-app directive was included in the body field. So the HTML body is under Angular JS. ng-model is added to the input field with the value empname.

In this example, as you notified the only new one is ng-model with the value in the input field. So, whenever you have added the ng-model property with the value (empname) and where you are using that value (empname) automatically it populates by entering the text into the respective input field.

Initially, when you run the HTML file, it is displayed like the following.

Now, whenever you have entered some value into the TextBox those values are automatically shown in the screen as output asynchronously.

You can run the sample application using the Plnkr tool at AngularJs Model.



ng-show and ng-hide

ng-show and ng-hide are also directives. These are very helpful when you want to hide or show something based on a property or object that either contains a value or does not.

As you seen in the aforesaid model example, the string is always displaying like "you entered employee name is:". You can change it so it will only display whenever the user has entered a value into a text box field.

For example, when you search for employee details, you need to display some message when the user begins searching by entering some employee name.

<!DOCTYPE html > < html > < head > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js" > </ script > </ head > < body ng-app > < h1 > Employee Search </ h1 > < form > < label > Employee Name: </ label > < input type = "text" placeholder = "Employee name to find" ng-model = "empname" /> < br /> < pre ng-show = "empname" > Employee Search Started..!!! </ pre > < pre ng-hide = "!empname" > You enetered employee name is: {{empname}} </ pre > </ form > </ body > </ html >



The previously described HTML file contains the following two pre tags:

The first one contains the ng-show directive with the value empname. So, The pre tag will be displayed whenever you have entered some text into the input filed that has applied the ng-mode is empname. The second one contains the ng-hide directive with the value !empname (here ! Indicates not). So, The pre tag will be hidden when the empname is empty.

in both of the preceding cases you can use any one of the directives (ng-show or ng-hide) for simplicity. But for the purpose of understanding, I have used both directives.

Now, you can run the HTML file and by default it displays like the following.

Now, you can enter any of the text in that text box field and then the screen will have the results shown as follows.

You can run the sample application using the Plnkr tool at AngularJS Show Hide.

Conclusion

I hope this article provides you an idea of the history and architecture of AngularJS, Binding Expressions and some of the directives like ng-app, ng-controller, ng-model, ng-show and ng-hide.