Professional Documents
Culture Documents
<script
src="https://ajax.googleapis.com/ajax/libs/ angularjs/1.6.4/angular.min.js">
</script>
ANGULAR JS EXAMPLE
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.4/angular.min.js
"></script>
<body>
<div ng-app="">
<! The ng-app directive tells AngularJS
that the <div> element is the "owner" of
an AngularJS application.>
<p ng-bind="name"></p>
<!The ng-bind directive binds the inner
HTML of the<p> element to the application
variable name. >
</div>
</body>
</html>
Name:
<!DOCTYPE html>
<html>
<head>
<style>
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
button.accordion.active, button.accordion:
hover {
background-color: #ddd;
}
button.accordion:after {
/* Unicode character for "plus" sign (+)
is '\02795' */
content: '\02795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
button.accordion.active:after {
content: "\2796";
}
div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: 0.6s ease-in-out;
opacity: 0;
}
div.panel.show {
opacity: 1;
max-height: 500px;
}
</style>
</head>
<body>
<h2>Accordion Example</h2>
<button class="accordion">Tiger</button>
<div class="panel">
<p>The tiger is the largest cat species,
most recognisable for their pattern of
dark vertical stripes on reddish-orange
fur with a lighter underside.</p>
</div>
<button class="accordion">Lion</button>
<div class="panel">
<p>The lion is one of the big cats in
the genus Panthera and a member of the family
Felidae.The commonly used term African lion
collectively denotes the several subspecies
in Africa.</p>
</div>
<button class="accordion">Cheetah</button>
<div class="panel">
<p>The cheetah, also known as the
hunting leopard, is a big cat that occurs
mainly in eastern and southern Africa and
a few parts of Iran</p>
</div>
<script>
</script>
</body>
</html>
In this example we have added a "plus" sign to each button. When the user
clicks on the button, the "plus" sign is replaced with a "minus" sign.
Tiger
Lion
Cheetah
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS EXPRESSIONS
AngularJS will resolve the expression, and return the result exactly
where the expression is written.
EXPRESSIONS EXAMPLE
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.
com/ajax/libs/angularjs/1.6.9/angular.
min.js"></script>
<body>
<div ng-app>
</div>
</div>
</body>
</html>
If you remove the ng-app directive, HTML will display the expression as it
is, without solving it. As you can see below.
ANGULAR JS NUMBERS
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/
ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<div/>
</body>
</html>
Total Amount: 10
Total Amount: 10
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
The ngModel directive binds an input ,select, textarea (or custom form
control) to a property on the scope using NgModelController, which is
created and exposed by this directive.
Binding the view into the model, which other directives such as input,
textarea or select require.
ngModel EXAMPLE
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.
js"></script>
<body>
</div>
<script>
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
TWO-WAY BINDING
One of the core feature of AngularJS which makes it popular is two way
data binding. In two way data binding, any changes to the model are
immediately reflected in the View and any changes in the View updates the
model.
The binding goes both ways. If the user changes the value inside the input
field, the AngularJS property will also change its value.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<script>
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS CONTROLLER
Explantion:
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
</div>
<script>
var app = angular.module('firstAPP',[]);
app.controller('myCtrl1',function($scope)
{
$scope.firstName = "Tom"
$scope.lastName = "Jerry";
});
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
Result For Above Example
First Name:
Last Name:
CONTROLLER METHODS
<!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('secondAPP',[]);
app.controller('Ctrl2', function($scope)
{
$scope.firstName = "Elon";
$scope.lastName = "Musk";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.
lastName;
};
});
angular.bootstrap(document.getElementById
("ID2"),
['secondAPP']);
</script>
</body>
</html>
<script src="https://ajax.googleapis.com/
ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<h1 ng-click="changeName()">
{{firstname}}</h1>
</div>
<script>
var app = angular.module('thirdAPP',[]);
app.controller('Ctrl3', function($scope) {
$scope.firstname = "Steve";
$scope.changeName = function() {
$scope.firstname = "Peter";
}
});
angular.bootstrap(document.getElementById
("ID3"), ['thirdAPP']);
</script>
</body>
</html>
Steve
Click on the header to run the "changeName" function.
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS SCOPES
The scope is the binding part between the HTML (view) and the JavaScript
(controller).The scope is an object with the available properties and
methods, is available for both the view and the controller.
Explantion:
USING SCOPES
When you make a controller in AngularJS, you pass the $scope object as
an argument.When adding properties to the $scope object in the controller,
the view (HTML) gets access to these properties.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<h1>{{bikename}}</h1>
</div>
<script>
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
Honda
The property "bikename" was made in the controller, and can be referred to
in the view by using the {{ }} brackets.
It is important to know which scope you are dealing with, at any time.In the
two examples above there is only one scope, so knowing your scope is not
an issue, but for larger applications there can be sections in the HTML
DOM which can only access certain scopes.
Each <li> element has access to the current repetition object, in this
case a string, which is referred to by using x.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<ul>
</ul>
</div>
<script>
app.controller('Ctrl2', function($scope)
{
$scope.names = ["Mark", "Alexander",
"Leo"];
});
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
</body>
</html>
Mark
Alexander
Leo
The variable "x" has a different value for each repetition, proving that each
repetition has its own scope.
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS SCOPES
The scope is the binding part between the HTML (view) and the JavaScript
(controller).The scope is an object with the available properties and
methods, is available for both the view and the controller.
Explantion:
When you make a controller in AngularJS, you pass the $scope object as
an argument.When adding properties to the $scope object in the controller,
the view (HTML) gets access to these properties.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<h1>{{bikename}}</h1>
</div>
<script>
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
Honda
The property "bikename" was made in the controller, and can be referred to
in the view by using the {{ }} brackets.
KNOW ABOUT SCOPE
It is important to know which scope you are dealing with, at any time.In the
two examples above there is only one scope, so knowing your scope is not
an issue, but for larger applications there can be sections in the HTML
DOM which can only access certain scopes.
Each <li> element has access to the current repetition object, in this
case a string, which is referred to by using x.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<ul>
</ul>
</div>
<script>
app.controller('Ctrl2', function($scope)
{
$scope.names = ["Mark", "Alexander",
"Leo"];
});
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
</body>
</html>
Mark
Alexander
Leo
The variable "x" has a different value for each repetition, proving that each
repetition has its own scope.
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS FILTERS
Filters format the value of an expression for display to the user. They can
be used in view templates, controllers or services. AngularJS comes with a
collection of built-in filters.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
</p>
</div>
<script>
angular.module('firstAPP',[]).controller
('Ctrl1', function($scope) {
$scope.firstName = "Tom",
$scope.lastName = "Jerry"
});
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
The filter Filter selects a subset of an array and can only be used on arrays,
and it returns an array containing only the matching items.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<ul>
<li ng-repeat="x in names | filter:'e'">
{{ x }}
</li>
</ul>
</div>
<script>
angular.module('secondAPP',[]).
controller('Ctrl2',function($scope) {
$scope.names = [
'Tiger',
'Cheetah',
'BlueWhale',
'Dugong',
'Orca',
'Elephant',
'Bison',
'Bear',
'Deer'
];
});
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
</body>
</html>
Tiger
Cheetah
BlueWhale
Elephant
Bear
Deer
This example displays only the names containing the letter "e".
By setting the ng-model directive on an input field, we can use the value of
the input field as an expression in a filter.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<ul>
</div>
<script>
angular.module('thirdAPP',[]).controller
('Ctrl3', function($scope) {
$scope.names = [
'Lion',
'Cheetah',
'Shark',
'Dugong',
'Orca',
'Elephant',
'Bison',
'Bear',
'Deer'
];
});
angular.bootstrap(document.getElementById
("ID3"), ['thirdAPP']);
</script>
</body>
</html>
Lion
Cheetah
Shark
Dugong
Orca
Elephant
Bison
Bear
Deer
You can make your own filters by registering a new filter factory function
with your module.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<ul>
<li ng-repeat="x in names">
{{x | myFilter}}
</li>
</ul>
</div>
<script>
app.controller('Ctrl4', function($scope)
{
$scope.names = [
'Lion',
'Cheetah',
'Shark',
'Dugong',
'Orca',
'Elephant',
'Bison',
'Bear',
'Deer'
];
});
angular.bootstrap(document.getElementById
("ID4"), ['fourthAPP']);
</script>
</body>
</html>
LiOn
ChEeTaH
ShArK
DuGoNg
OrCa
ElEpHaNt
BiSoN
BeAr
DeEr
Type the code or copy/paste below and press show result to see the result
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
> </script>
<body>
<select ng-model="selectedName"
ng-options="x for x in names">
</select>
</div>
<script>
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
Result For Above Example
TigerLionPuma
This example shows how to fill a dropdown list using the ng-options
directive.
You can also use the ng-repeat directive to make the same dropdown list,
the ng-repeat directive repeats a block of HTML code for each item in an
array, it can be used to create options in a dropdown list, but the ng-options
directive was made especially for filling a dropdown list with options
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<select>
</select>
</div>
<script>
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
</body>
</html>
This example shows how to fill a dropdown list using the ng-repeat
directive.
In the below example, selected value will always be the value in a key-
value pair, the value in a key-value pair can also be an object. The options
in the dropdown list does not have to be the key in a key-value pair, it can
also be the value, or a property of the value object.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<b>Select a car:</b>
<select ng-model="selectedCar"
ng-options="y.brand
for (x, y) in cars">
</select>
<h1>You selected:{{selectedCar.brand}}
</h1>
<h2>Model: {{selectedCar.model}}</h2>
<h3>Color: {{selectedCar.color}}</h3>
<hr>
<b>Select a car:</b>
<select ng-model="selectedCar1"
ng-options="x for
(x, y) in cars">
</select>
</div>
<script>
angular.bootstrap(document.getElementById
("ID3"), ['thirdAPP']);
</script>
</body>
</html>
Result For Above Example
Select a car: TeslaFordVolvo
You selected:
Model:
Color:
The visible text inside the dropdown list can also be a property of the value
object.
Select a car: car01car02car03
You selected:
Model:
Color:
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS EVENTS
AngularJS has its own HTML events directives, the event directives allows
us to run AngularJS functions at certain user events. An AngularJS event
will not overwrite an HTML event, both events will be executed.
You can add AngularJS event listeners to your HTML elements by using
one or more of these directives:
ng-blur
ng-change
ng-click
ng-copy
ng-cut
ng-dblclick
ng-focus
ng-keydown
ng-keypress
ng-keyup
ng-mousedown
ng-mouseenter
ng-mouseleave
ng-mousemove
ng-mouseover
ng-mouseup
ng-paste
MOUSE EVENTS
Mouse events occur when the cursor moves over an element or when a
mouse button is clicked on an element. Mouse events can be added on any
HTML element.
ng-mouseover
ng-mouseenter
ng-mousemove
ng-mouseleave
ng-mousedown
ng-mouseup
ng-click
<body>
</div>
<script>
var app = angular.module('firstAPP', []);
app.controller('Ctrl1', function($scope) {
$scope.count = 0;
});
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
</div>
<script>
var app = angular.module('secondAPP',[]);
app.controller('Ctrl2', function($scope)
{
$scope.count = 0;
});
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
</body>
</html>
0
TOGGLE
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
> </script>
<body>
<div ng-show="show">
<h1>Animals:</h1>
<div>Tiger</div>
<div>Lion</div>
<div>Cheetah</div>
</div>
</div>
<script>
var app = angular.module('thirdAPP',[]);
app.controller('Ctrl3', function($scope)
{
$scope.show = false;
$scope.myFunc = function() {
$scope.show = !$scope.show;
}
});
angular.bootstrap(document.getElementById
("ID3"), ['thirdAPP']);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<h1 ng-mousemove="myFunc($event)">
Mouse Over Me!
</h1>
</div>
<script>
angular.bootstrap(document.getElementById
("ID4"), ['fourthAPP']);
</script>
</body>
</html>
Result For Above Example
Mouse over the heading to display the value of clientX and clientY from the
event object.
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULARJS API
Comparing objects
Iterating objects
Converting data
The Global API functions are accessed using the angular object.
API Description
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<h2>angular.lowercase()</h2>
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
<h2>angular.uppercase()</h2>
<p>{{ x3 }}</p>
<p>{{ x4 }}</p>
</div>
<script>
var app = angular.module('firstAPP',[]);
app.controller('Ctrl1', function($scope)
{
$scope.x1 ="TOMMY";
$scope.x2 =angular.lowercase($scope.x1);
$scope.x3 ="peter";
$scope.x4 =angular.uppercase($scope.x3);
});
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
TOMMY
tommy
angular.uppercase()
peter
PETER
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.js"
></script>
<body>
<h2>angular.isString()</h2>
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
<h2>angular.isNumber()</h2>
<p>{{ x3 }}</p>
<p>{{ x4 }}</p>
</div>
<script>
var app = angular.module('secondAPP',[]);
app.controller('Ctrl2', function($scope)
{
$scope.x1 ="JOHN";
$scope.x2 =angular.isString($scope.x1);
$scope.x3 =24;
$scope.x4 =angular.isNumber($scope.x3);
});
angular.bootstrap(document.getElementById
("ID2"), ['secondAPP']);
</script>
</body>
</html>
JOHN
true
angular.isNumber()
24
true
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result
ANGULAR JS ANIMATION
<body ng-app="ngAnimate">
What Does ngAnimate Do?
The ngAnimate module adds and removes classes. The ngAnimate module
does not animate your HTML elements, but when ngAnimate notice certain
events, like hide or show of an HTML element, the element gets some pre-
defined classes which can be used to make animations.
ng-show
ng-hide
ng-class
ng-view
ng-include
ng-if
ng-switch
The ng-repeat directive also adds a ng-move class value when the HTML
element changes position. In addition, during the animation, the HTML
element will have a set of class values, which will be removed when the
animation has finished. Example: the ng-hide directive will add these class
values.
ng-animate
ng-hide-animate
ng-hide-add
ng-hide-remove
ng-hide-add-active
ng-hide-remove-active
CSS TRANSITIONS
CSS transitions allows you to change CSS property values smoothly, from
one value to another, over a given duration.
<!DOCTYPE html>
<html>
<style>
.one{
transition: all linear 0.5s;
background-color: turquoise;
height: 100px;
}
.ng-hide {
height: 0;
}
</style>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.
js"></script>
<script src="https://ajax.googleapis.com/
ajax/libs/angularjs/1.6.9/angular-
animate.js"></script>
<body ng-app="firstAPP">
<script>
var app=angular.module('firstAPP',
['ngAnimate']);
angular.bootstrap(document.getElementById
("ID1"), ['firstAPP']);
</script>
</body>
</html>
Result For Above Example
When the DIV element gets the .ng-hide class, the transition will take 0.5
seconds, and the height will smoothly change from 100px to 0.
CSS ANIMATIONS
CSS Animations allows you to change CSS property values smoothly, from
one value to another, over a given duration.
<!DOCTYPE html>
<html>
<style>
@keyframes myChange {
from {
height: 100px;
} to {
height: 0;
}
}
div{
height: 100px;
background-color: lightblue;
}
div.ng-hide {
animation: 0.5s myChange;
}
</style>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular.min.
js"></script>
<script src="https://ajax.googleapis.com
/ajax/libs/angularjs/1.6.9/angular-
animate.js"></script>
<body ng-app="ngAnimate">
<div ng-hide="myCheck">
</div>
</body>
</html>
When the DIV element gets the .ng-hide class, the myChange animation
will run, which will smoothly change the height from 100px to 0.
TRY YOURSELF :
Type the code or copy/paste below and press show result to see the result