CH 1 AngularJS
CH 1 AngularJS
What is AngularJS?
AngularJS is a modern JavaScript framework from
Google, commonly used to work with Single Page
Applications (SPAs).
AngularJS is primarily based on declarative HTML
attributes that you can add to your HTML page with
a <script> tag.
The library is called Angular because HTML uses
angular-shaped brackets.
AngularJS is open sourced.
Introduction
VIEW
View in an application 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 the DOM,
composed of directives, filters and data-bindings.
Model-View-Controller (MVC)
CONTROLLER
Controller holds all of our application logic. In
AngularJS the controllers are JavaScript classes.
The Controller controls and prepares the data into
the form so that it can be rendered at the View.
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-Controller (MVC)
MODEL
Model is the bridge standing between Controllers and
Views.
In AngularJS, the model data is stored in Object
properties.
There can be a controller which we can bind to two or
more views. In reality the Controller is blank about views
and has no information about the views
Similarly View is independent of logic implemented or
data present in the Controller.
And, model acts as the communication tunnel between
the Views and Controller.
Hello, World : Example
Hello.html
<!DOCTYPE html>
<html ng-app>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.mi
n.js"></script>
<script src="controllers.js"></script>
</head>
<body>
<div ng-controller = 'HelloController'>
<p>{{greeting.text}}, World</p>
</div>
</body>
</html>
Hello, World : Example
controller.js
function HelloController ($scope) {
$scope.greeting = {text: 'Hello'};
}
Note that the part of DOM is updated separately,
rather than updating the whole page.
Here, we have merged template HTML strings with
data, and inserted the result where we want it in the
DOM.
Data binding
<!DOCTYPE html>
<html ng-app>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js">
</script>
<script src="controllers.js"></script>
</head>
<body>
<div ng-controller = 'HelloController'>
<input ng-model='greeting.text'>
<p>Hello {{greeting.text}}!</p> See: Controller.js
</div>
function HelloController ($scope) {
</body>
</html> $scope.greeting = {text: 'Students'};
See: HelloDynamic.html }
Basic building blocks of AngularJS
Invoking AngularJS
Any application must do two things to start Angular:
1. Load the angular.js library
2. Tell Angular which part of the DOM it should manage with
the ng-app directive
Loading the Script:
You can load the script from Google’s content
delivery network (CDN), like so:
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js">
</script>
Directives
ng-app
The ng-app directive defines an AngularJS
application.
We use this directive to auto-bootstrap an AngularJS
application.
The ng-app directive designates the root element of
the application and is typically placed near the root
element of the page - e.g. on the <body> or <html>
tags.
If ng-app directive is found, that will become the
root scope of the app.
ng-app : Syntax
<element ng-app="modulename">
...
content inside the ng-app root element can contain
AngularJS code
...
</element>
<body ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</body>
modulename is Optional, Specifies the name of a module
to load with the application.
See ng-appEx.html
Directives
ng-controller
In AngularJs, Controllers are the JavaScript classes
used to manage the areas of the page.
AngularJS applications are controlled by controllers.
The ng-controller directive defines the application
controller.
ng-controller : Example
</div>
<script>
var appModule = angular.module('myApp', []);
appModule.controller('myCtrl', function($scope) {
$scope.firstName = “Sanjay";
$scope.lastName = “Bhakkad";
});
</script> See NameEx.html
Code Explained
Controller Methods
The example above demonstrated a controller object
with two properties: firstName and lastName.
A controller can also have methods (variables as
functions):
Controller with method : Example
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = “Sanjay";
$scope.lastName = “Bhakkad";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>
See FullNameEx.html
ng-controller : Example
</div>
<script src="personController.js"></script>
See NameExternalCtrlEx.html
Directives
ng-init
The ng-init directive initializes application data.
Example:
</div>
See ng-initEx.html
Directives
Data Bindings
Data binding in AngularJS is the synchronization between the
model and the view.
Data Model
AngularJS applications usually have a data model. The data
model is a collection of data available for the application.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = “Sanjay";
});
HTML View
The HTML container where the AngularJS application is
displayed, is called the view.
The view has access to the model, and there are several ways
of displaying model data in the view, as:
Directives
HTML View
1. You can use the ng-bind directive, which will bind the
innerHTML of the element to the specified model property:
<p ng-bind="firstname"></p>
2. You can also use double braces {{ }} to display content from
the model:
<p>{{firstname}}</p>
3. Or you can use the ng-model directive on HTML controls to
bind the model to the view.
<input ng-model="firstname">
See dataBindingsEx.html
Directives
Data Binding
Data binding shown via {{ }} interpolation notation lets us insert
the value of a variable into part of the page and keep it in sync.
The {{ }} sets up a one-way relationship that says “insert a value
here.”
If you want to keep changes in sync with your model use ng-
model.
The {{ firstName }} expression, in the example above, is an
AngularJS data binding expression.
Data binding in AngularJS binds AngularJS expressions with
AngularJS data.
E.g.: {{ firstName }} is bound with ng-model="firstName".
Directives
ng-model
The ng-model directive binds the value of HTML
controls (input, select, textarea) to application data.
The ng-model directive can also:
Provide type validation for application data (number, email,
required).
Provide status for application data (invalid, error).
Provide CSS classes for HTML elements.
Bind HTML elements to HTML forms.
ng-model : Example (Static)
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "Sanjay Bhakkad";
});
</script>
See ng-modelEx.html
ng-model : Example (Two Way Bindings)
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = “Sanjay Bhakkad";
});
</script>
See ng-modelEx2.html
Two Way Bindings
ng-repeat
The ng-repeat directive repeats a set of HTML, a
given number of times.
The set of HTML will be repeated once per item in a
collection.
The collection must be an array or an object.
Note: Each instance of the repetition is given its
own scope, which consist of the current item.
ng-repeat : Example 1
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<body>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = ["Sanjay Bhakkad",
"Mahesh Potdar",
"U. H. Nagarkar",
"Hari Shankar Rai"]
});
</script>
</body>
See ngRepeatEx.html
ng-repeat : Example 3
Using External Controller
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script src="recordsCtrl.js"></script>
<table>
<tr>
<th>No.</th>
<th>Name</th>
<th>Age</th> var app = angular.module("myApp", []);
</tr> app.controller("myCtrl", function($scope) {
<tr ng-repeat="x in records">
$scope.records = [
<td>{{ $index + 1 }}</td>
<td>{{ x.name}}</td>
{name:'Sanjay Bhakkad', age:40},
<td>{{ x.age}}</td> {name:'Mahesh Potdar', age:45},
</tr> {name:'U. H. Nagarkar', age:50},
</table> {name:'Hari Shankar Rai', age:35}]
</body>
});
</html>
See RepeatEx3.html File: recordsCtrl.js
AngularJS : Filters
LowerCaseUpperCaseFilters.html
DateFilter.html
CurrencyFilter.html
LimitToFilter.html
OrderByFilter.html
Filter_Filter.html
NumberFilter.html
JsonFilter.html