AngularJS is a structured, very powerful JavaScript based open-source front end web framework. It is used for one-page dynamic applications. It is maintained by Google and by a community of individuals and corporations to address many of the challenges encountered in developing single-page applications. Single page means the webpages need to be refreshed. We can further divide a webpage into different components. It is used by thousands of developers around the world. It is mainly used for creating the web applications. It was originally developed in 2009 by Misko Hevery and Adam Abrons.
AngularJS injects the required components to the same webpage, i.e. the webpage becomes dynamic. And when the user navigates to a URL, AngularJS injects the required components into the same page instead of refreshing the page. It essentially reuses the components which do not change, i.e. reducing the load time.
Short, AngularJS is a framework to build large scale, high-performance, and easy tomaintain web applications.
The CDN link for Angular JS can be taken from the web reference of Google itself.
https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js
This link can be used in the script tag as follows:
Code:
Examples
Displaying a welcome message dynamically
Welcome {{name}}
Output: Enter your name in the box and see what happens.
AngularJS expressions are those that are written inside double braces {{expression}}.AngularJS evaluates the specified expression and binds the result data to HTML. AngularJS expressions can also be written inside a directive: ng-bind="expression".
AngularJS expression can contain literals, operators and variables. AngularJS displays the data exactly at the place where the expression is placed.
For example, an expression {{6/2}} will produce the result 3 and will be bound to HTML.
Code:
Examples
Displaying how angular evaluates expression within the HTML code.
Addition: 10 + 2 = {{10+2}}
Subtraction: 10 - 2 = {{10-2}}
Multiplication: 10 * 2 = {{10*2}}
Division: 10 / 2 = {{10/2}}
Output:
For Example, let’s take the above code and remove the ng-app directive:
Code:
Examples
Displaying how angular evaluates expression within the HTML code.
Addition: 10 + 2 = {{10+2}}
Subtraction: 10 + 2 = {{10-2}}
Multiplication: 10 + 2 = {{10*2}}
Division: 10 + 2 = {{10/2}}
Output:
In AngularJS, numbers can be used in expressions. Any expression using the number and the operators (like , +, -, *, %, etc) then these are called number expressions. We may use ng-init directive to declare and initialize the variables in AngularJs. For example: ng-init="RateOfInterest=5;PrincipalAmount=20000"
Code:
Examples
Calculating simple interest.
Total interest = {{(principalAmount * rateOfInterest * time) / 100}}
Output:
The string expression in Angularjs is a unit of code to perform operations on string values. It may be simply adding two stings i.e. concatenation etc.
Code:
Examples
String expressions.
My full name is {{firstName + " " + lastName}}
Output:
AngularJs expressions can have objects as well. The object expressions in AngularJs hold object properties in them and the same way then evaluates at the view where they are used.
For Example, lets define one object as a personname object having 2 key value pairs of "firstName" and "lastName". personname object may be defined using ng-init directive like ng-init="personname={firstName:'Shashi',lastName:'Kant'}"
Code:
Examples
Accessing objects.
My full name is {{personname.firstName + " " + personname.lastName}}
Output:
Expressions can be used to work with arrays as well. Array expressions in AngularJs are the expression that hold an array and use those array objects while evaluating array expressions. For Example, marks array may hold Marks value of various subject (here 4 subjects) and it may be defined using ng-init directive as ng-init="marks=[85,80,75]".
Code:
Examples
Accessing objects.
You have got {{marks[0]}} in Physics.
You have got {{marks[1]}} in Chemistry.
You have got {{marks[2]}} in Biology.
Output:
Expressions can be used to work with arrays as well. Array expressions in AngularJs are the expression that hold an array and use those array objects while evaluating array expressions. For Example, marks array may hold Marks value of various subject (here 4 subjects) and it may be defined using ng-init directive as ng-init="marks=[85,80,75]".
Code:
Examples
Accessing many datatypes together.
Total interest = {{(principalAmount * rateOfInterest * time) / 100}}
My full name is {{firstName + " " + lastName}}
My full name is {{personname.firstName + " " + personname.lastName}}
You have got {{marks[0]}} in Physics.
You have got {{marks[1]}} in Chemistry.
You have got {{marks[2]}} in Biology.
Output: