Angular Js
Angular Js
JS
What is AngularJS
Angular JS is an open source JavaScript framework that is used to build web applications.
It can be freely used, changed and shared by anyone.
It is an excellent framework for building single phase applications and line of business
applications.
AngularJS extends HTML attributes with Directives, and binds data to HTML
with Expressions.
Advantage of AngularJS
There are a lot of JavaScript frameworks for building web applications. So, it is a genuine
question, why to use Angular JS.
o Dependency Injection: Dependency Injection specifies a design pattern in which
components are given their dependencies instead of hard coding them within the
component.
o Two way data binding: AngularJS creates a two way data-binding between the
select element and the orderProp model. orderProp is then used as the input for
the orderBy filter.
o Testing: Angular JS is designed in a way that we can test right from the start. So,
it is very easy to test any of its components through unit testing and end-to-end
testing.
o Model View Controller: In Angular JS, it is very easy to develop application in a
clean MVC way. You just have to split your application code into MVC
components i.e. Model, View and the Controller.
o Directives, filters, modules, routes etc.
1. Open-Source Framework – Since AngularJS is an open-source framework, you can
enjoy and experience minimal issues or loopholes. This is one of the most essential
benefits of AngularJS.
2. Provides Two-Way Binding - Be it the previous version or Angular latest version, the
framework always helps to synchronize the data and presentation layer. Because of this
feature, you'll never need to write additional JavaScript code just to keep the data in your
HTML code and your data layer coordinated. AngularJS framework automatically does
this for you. You only require defining which control relies on which component of your
Model.
3. Material Design Library – Angular JS has a remarkable Angular material library with
several attractive designs. These material designs assist software developers in
developing responsive and valuable user interfaces.
4. Lightweight Web Apps - The earlier versions of the AngularJS framework had a
significant size problem that limits fast loading. Angular's latest versions now consist of
enhanced functionalities; this helps to make a lighter web app in comparison to other
models. Also, to make a web app more efficient, you can opt for some of the best Angular
frameworks.
5. Different Testing Techniques – AngularJS development supports both unit testing and
integration testing.
● D a ta - B in d in g – T h e p r o ce ss is a u to m a tic d a ta
sy n ch r o n iz a tio n b e tw e e n M o d e l a n d Vie w e le m e n ts.
● D ir e ct iv e s – T h e y a r e m a r k e r s o n D O M co m p o n e n ts.
So ftw a r e d e v e lo p e r s u se th e m t o p r o d u ce cu sto m H T M L
ta g s. An g u la r JS fr a m e w o r k co n sists o f in b u ilt co m m a n d s.
● F ilte r s – T h e se h e lp to ch o o se su b se ts fo r d iffe r e n t ite m s
fr o m a n a r r a y a n d r e tu r n a fr e sh n e w a r r a y .
● R o u tin g – It is a n An g u la r JS co n ce p t fo r sw itch in g v ie w s.
● T e m p la t e s – All th e d a t a fr o m th e M o d e l a n d C o n tr o lle r
a r e sto r e d h e r e , a lso k n o w n a s t h e r e n d e r e d Vie w .
● D e e p L in k in g – It e n a b le s d e v e lo p e r s t o e n co d e th e
a p p lica t io n sta te in th e U R L . T h is is n e ce ssa r y fo r
b o o k m a r k in g . Also , y o u ca n q u ick ly r e sto r e th e
a p p lica t io n in t h e sa m e co n d it io n w ith t h e h e lp o f a U R L .
● D e p e n d e n cy In je ctio n – An g u la r JS fr a m e w o r k co n sists o f
a n in b u ilt d e p e n d e n cy in je ctio n w h ich h e lp s d e v e lo p e r s
w ith se a m le ss so ftw a r e d e v e lo p m e n t.
● M VVM – T a k e n o te : An g u la r JS d o e s n o t im p le m e n t a n d
in te g r a t e M VC in its co n v e n tio n a l fo r m . In ste a d , it u se s
a n a p p r o a ch clo se r to M VVM (M o d e l- Vie w - Vie w M o d e l).
H u m o r o u sly it is a lso k n o w n a s M o d e l Vie w W h a te v e r .
L im it a t io n s o f An gu la r JS
1. St eep Learning Curve - AngularJS framework has a high learning curve. A
software developer well-versed in JavaScript concepts would find it
challenging to grasp AngularJS concepts speedily. At a point, they would
discover ReactJS to be much more effortless to deploy. However, you need not
worry. If you want to opt for AngularJS, our software developers will make
your work much more manageable.
MVC is popular because it isolates the application logic from the user
interface layer and supports separation of concerns. The controller receives
all requests for the application and then works with the model to prepare any
data needed by the view. The view then uses the data prepared by the
controller to generate a final presentable response.
When you open the link https://angularjs.org/, you will see there are two
options to download AngularJS library −
<body>
<h1>Sample Application</h1>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
</body>
</html>
The ng-app directive tells AngularJS that the <div> element is the "owner"
of an AngularJS application.
The ng-model directive binds the value of the input field to the application
variable name.
The ng-bind directive binds the content of the <p> element to the
application variable name.
ng-app directive
The ng-app directive starts an AngularJS Application. It defines the root
element. It automatically initializes or bootstraps the application when the
web page containing AngularJS Application is loaded. It is also used to load
various AngularJS modules in AngularJS Application. In the following
example, we define a default AngularJS application using ng-app attribute
of a <div> element.
<div ng-app = "">
...
</div>
ng-init directive
The ng-init directive initializes an AngularJS Application data. It is used to
assign values to the variables.
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
</div>
ng-repeat directive
The ng-repeat directive repeats HTML elements for each item in a
collection.
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
</body>
</html>
ã z§̉ J ì0? �┘�( ®ê ìXî î Äç ã î
In AngularJS, expressions are used to bind application data to HTML.
AngularJS resolves the expression, and return the result exactly where the
expression is written. AngularJS expressions are pure JavaScript
expressions and output the data where they are used.
Expressions are written inside double braces {{expression}}.They can also
be written inside a directive.
Ex: ng-bind="expression".
Ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.j
s"></script>
<body>
<div ng-app>
<p>A simple expression example: {{ 5 + 5 }}</p>
</div>
</body>
</html>
Ex2.
AngularJS Numbers
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angula
r.min.js"></script>
<body>
<div ng-app="" ng-init="quantity=5;cost=5">
<p>Total in dollar: {{ quantity * cost }}</p>
</div>
</body>
</html>
ex:
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
</div>
</body>
</html>
AngularJS Strings
ex.without bind
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
</div>
</body>
</html>
ex.with ng-bind
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
<p>My full na me is : <s pan ng-bind="firs tName + ' ' + las tNa me"></s pan></p>
</div>
</body>
</html>
AngularJS Objects
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
<div ng-a pp="" ng-init="pers on={firs tNa me:'Sonoo',la s tNa me:'J a is wa l'}">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
<div ng-a pp="" ng-init="pers on={firs tNa me:'Sonoo',la s tNa me:'J a is wa l'}">
</body>
</html>
AngularJS Arrays
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<s cript
s rc="http:/ / a ja x.googlea pis .com/ a ja x/ libs / angula rjs / 1.4.8/ a ngula r.min.js ">
</s cript>
<body>
</div>
</body>
</html>
AngularJS Scope
The scope is the binding part between the HTML (view) and the JavaScript
(controller).
The scope is available for both the view and the controller.
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{carname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.carname = "Volvo";
});
</script>
<p>The property "carname" was made in the controller, and can be referred to
in the view by using the {{ }} brackets.</p>
</body>
</html>
When adding properties to the $scope object in the controller, the view
(HTML) gets access to these properties.
In the view, you do not use the prefix $scope, you just refer to a property
name, like {{carname}}.
The scope is a JavaScript object with properties and methods, which are
available for both the view and the controller.
ex:If you make changes in the view, the model and the controller will be
updated
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></s
cript>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<input ng-model="name">
<h1>My name is {{name}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
<p>When you change the name in the input field, the changes will affect the
model, and it will also affect the name property in the controller.</p>
</body>
</html>
AngularJS Controllers
AngularJS controllers are used to control the flow of data of AngularJS
application. A controller is defined using ng-controller directive. A controller
is a JavaScript object containing attributes/properties and functions. Each
controller accepts $scope as a parameter which refers to the
application/module that controller is to control. A controller is a JavaScript
Object, created by a standard JavaScript object constructor
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
ng-app="myApp".
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "Siya";
$scope.lastName = "Sharma";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
</div>
<script src="namesController.js"></script>
</body>
</html>
namesController.js file
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}
];
});
AngularJS Filters
In AngularJS, filters are used to format data. Following is a list of filters
used for transforming data.
Filter Description
Currency It forma ts a number to a currency forma t.
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "Shriya",
$scope.lastName = "Mehta"
});
</script>
</body>
</html>
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
</li>
</ul>
</div>
<script>
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
});
</script>
</body>
</html>
The currency Filter
The currency filter formats a number as currency:
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
</div>
<script>
app.controller('costCtrl', function($scope) {
$scope.price = 58;
});
</script>
</body>
</html>
The filter Filter
The filter filter selects a subset of an array.
The filter filter can only be used on arrays, and it returns an array
containing only the matching items.
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<ul>
{{ x }}
</li>
</ul>
</div>
<script>
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
<p>This example displays only the names containing the letter "i".</p>
</body>
</html>
Type a letter in the input field, and the list will shrink/grow depending on the
match:
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<ul>
{{ x }}
</li>
</ul>
</div>
<script>
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
</body>
</html>
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<tr>
<th ng-click="orderByMe('name')">Name</th>
<th ng-click="orderByMe('country')">Country</th>
</tr>
<td>{{x.name}}</td>
<td>{{x.country}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
});
</script>
</body>
</html>
Custom Filters
You can make your own filters by registering a new filter factory function
with your module
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
{{x | myFormat}}
</li>
</ul>
<script>
app.filter('myFormat', function() {
return function(x) {
c = x[i];
if (i % 2 == 0) {
c = c.toUpperCase();
txt += c;
return txt;
};
});
app.controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
</body>
</html>
AngularJS $http
$http is an AngularJS service for reading data from remote servers.
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.j
s"></script>
<body>
<h1>{{myWelcome}}</h1>
</div>
<p>The $http service requests a page on the server, and the response is
set as the value of the "myWelcome" variable.</p>
<script>
$http.get("welcome.htm")
.then(function(response) {
$scope.myWelcome = response.data;
});
});
</script>
</body>
</html>
Methods
The example above uses the .get method of the $http service.
The .get method is a shortcut method of the $http service. There are several
shortcut methods:
● .delete()
● .get()
● .head()
● .jsonp()
● .patch()
● .post()
● .put()
The methods above are all shortcuts of calling the $http service:
AngularJS Tables
The ng-repeat directive is perfect for displaying tables.
ex:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<table>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
table, th , td {
border-collapse: collapse;
padding: 5px;
table tr:nth-child(odd) {
background-color: #f1f1f1;
table tr:nth-child(even) {
background-color: #ffffff;
</style>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angul
ar.min.js"></script>
<body>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
AngularJS Forms
Forms in AngularJS provides data-binding and validation of input controls.
Input Controls
Input controls are the HTML input elements:
● input elements
● select elements
● button elements
● textarea elements
●
Data-Binding
Input controls provides data-binding by using the ng-model directive.