AngularJS Tutorial
AngularJS Tutorial
This Tutorial
This tutorial is specially designed to help you learn AngularJS as quickly and
efficiently as possible.
First, you will learn the basics of AngularJS: directives, expressions, filters,
modules, and controllers.
Then you will learn everything else you need to know about AngularJS:
AngularJS Example
<!DOCTYPE html>
<html lang="en-US">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
<body>
<div ng-app="">
<p>Name : <input type="text" ng-model="name"></p>
<h1>Hello {{name}}</h1>
</div>
</body>
</html>
What You Should Already Know
Before you study AngularJS, you should have a basic understanding of:
HTML
CSS
JavaScript
AngularJS History
AngularJS version 1.0 was released in 2012.
The idea turned out very well, and the project is now officially supported by
Google.
AngularJS Introduction
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
</body>
</html>
Example explained:
AngularJS Directives
As you have already seen, AngularJS directives are HTML attributes with
an ng prefix.
<div ng-app="" ng-init="firstName='John'">
</div>
<div data-ng-app="" data-ng-init="firstName='John'">
</div>
You can use data-ng-, instead of ng-, if you want to make your page HTML
valid.
AngularJS Expressions
AngularJS expressions are written inside double braces: {{ expression }}.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS expressions bind AngularJS data to HTML the same way as
the ng-bind directive.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
<body>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p>{{name}}</p>
</div>
</body>
</html>
AngularJS Applications
AngularJS modules define AngularJS applications.
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>
AngularJS Module
AngularJS Controller
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
AngularJS Expressions
AngularJS binds data to HTML using Expressions.
AngularJS Expressions
AngularJS expressions can be written inside double
braces: {{ expression }}.
AngularJS will resolve the expression, and return the result exactly where the
expression is written.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
<body>
<div>
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
You can write expressions wherever you like, AngularJS will simply resolve
the expression and return the result.
Change the color of the input box below, by changing its value:
<div ng-app="" ng-init="myCol='lightblue'">
<input style="background-color:{{myCol}}" ng-model="myCol">
</div>
AngularJS Numbers
AngularJS numbers are like JavaScript numbers:
<div ng-app="" ng-init="quantity=1;cost=5">
</div>
Same example using ng-bind:
<div ng-app="" ng-init="quantity=1;cost=5">
</div>
AngularJS Strings
AngularJS strings are like JavaScript strings:
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
</div>
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
</div>
Same example using ng-bind:
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
</div
AngularJS Arrays
AngularJS arrays are like JavaScript arrays:
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
AngularJS Expressions vs. JavaScript
Expressions
Like JavaScript expressions, AngularJS expressions can contain literals,
operators, and variables.
AngularJS Modules
An AngularJS module defines an application.
Creating a Module
A module is created by using the AngularJS function angular.module
<div ng-app="myApp">...</div>
<script>
</script>
Now you can add controllers, directives, filters, and more, to your AngularJS
application.
Adding a Controller
Add a controller to your application, and refer to the controller with the ng-
controller directive:
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
Adding a Directive
AngularJS has a set of built-in directives which you can use to add
functionality to your application.
In addition you can use the module to add your own directives to your
applications:
<div ng-app="myApp" w3-test-directive></div>
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
template : "I was made in a directive constructor!"
};
});
</script>
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>
<script src="myApp.js"></script>
<script src="myCtrl.js"></script>
</body>
</html>
myApp.js
var app = angular.module("myApp", []);
The [] parameter in the module definition can be used to define dependent
modules.
myCtrl.js
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName= "Doe";
});
AngularJS modules reduces this problem, by keeping all functions local to the
module.
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
AngularJS Directives
AngularJS lets you extend HTML with new attributes called Directives.
AngularJS Directives
AngularJS directives are extended HTML attributes with the prefix ng-.
<div ng-app="" ng-init="firstName='John'">
<p>Name: <input type="text" ng-model="firstName"></p>
<p>You wrote: {{ firstName }}</p>
</div>
Data Binding
The {{ firstName }} expression, in the example above, is an AngularJS data
binding expression.
Data binding in AngularJS binds AngularJS expressions with AngularJS data.
In the next example two text fields are bound together with two ng-model
directives:
<div ng-app="" ng-init="quantity=1;price=5">
Quantity: <input type="number" ng-model="quantity">
Costs: <input type="number" ng-model="price">
</div>
Using ng-init is not very common. You will learn how to initialize data
in the chapter about controllers.
<div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
<ul>
<li ng-repeat="x in names">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
Normally, you will not use ng-init. You will use a controller or module
instead.
To invoke the new directive, make an HTML element with the same tag name
as the new directive.
<w3-test-directive></w3-test-directive>
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
template : "<h1>Made by a directive!</h1>"
};
});
</script>
</body>
Element name
Attribute
Class
Comment
Element name
<w3-test-directive></w3-test-directive>
Attribute
<div w3-test-directive></div>
Class
<div class="w3-test-directive"></div>
Comment
Restrictions
You can restrict your directives to only be invoked by some of the methods.
By default the value is EA, meaning that both Element names and attribute names can invoke the directive.
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
Two-Way Binding
The binding goes both ways. If the user changes the value inside the input
field, the AngularJS property will also change its value:
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
<form ng-app="" name="myForm">
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">Not a valid e-mail
address</span>
</form>
In the example above, the span will be displayed only if the expression in
the ng-show attribute returns true.
Application Status
The ng-model directive can provide status for application data (valid, dirty,
touched, error):
<form ng-app="" name="myForm" ng-init="myText = '[email protected]'">
Email:
<input type="email" name="myAddress" ng-model="myText" required>
<h1>Status</h1>
{{myForm.myAddress.$valid}}
{{myForm.myAddress.$dirty}}
{{myForm.myAddress.$touched}}
</form>
CSS Classes
The ng-model directive provides CSS classes for HTML elements, depending
on their status:
<style>
input.ng-invalid {
background-color: lightblue;
}
</style>
<body>
<form ng-app="" name="myForm">
Enter your name:
<input name="myName" ng-model="myText" required>
</form>
The ng-model directive adds/removes the
following classes, according to the status of the
form field:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine
AngularJS Data Binding
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.
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.
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>
You can also use double braces {{ }} to display content from the model:
<p>First name: {{firstname}}</p>
The ng-model Directive
Use the ng-model directive to bind data from the model to the view on HTML
controls (input, select, textarea)
<input ng-model="firstname">
Two-way Binding
Data binding in AngularJS is the synchronization between the model and the
view.
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
AngularJS Controller
Applications in AngularJS are controlled by controllers. Read about controllers
in the AngularJS Controllers chapter.
Because of the immediate synchronization of the model and the view, the
controller can be completely separated from the view, and simply
concentrate on the model data. Thanks to the data binding in AngularJS, the
view will reflect any changes made in the controller.
<div ng-app="myApp" ng-controller="myCtrl">
<h1 ng-click="changeName()">{{firstname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.changeName = function() {
$scope.firstname = "Nelly";
}
});
</script>