AngularJs Tutorials
Important News
Expression
Example :-
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="">
<p>Sum of Two number : {{ 5 + 5 }}</p>
</div>
</body>
</html>
Output :-
sum of two number : 10
If you remove the ng-app directive from div of HTML will display the expression as it is.
Example :
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<p>Without the ng-app directive, HTML will display the expression as it is, without solving it.</p>
<div>
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
Output :-
My first expression: {{ 5 + 5 }}
Example: Let AngularJS change the value of CSS properties.
Change the color of the input box below, by changing it's value:
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<p>Change the value of the input field, then change the color of input field.</p>
<div ng-app="" ng-init="col='lightblue'">
<input style="background-color:{{col}}" ng-model="col" value="{{col}}">
</div>
</body>
</html>
Output :-
The background color of the input box will be whatever you write in the input field.
ng-init :-
The ngInit directive allows you to evaluate an expression in the current scope.
The ng-init directive evaluates the given expression(s).
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="quantity=1;cost=200">
<p>Total cost = {{ quantity * cost }}</p>
</div>
</body>
</html>
Output :-
Total cost = 200
Example :-
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total cost = <span ng-bind="quantity * cost"></span></p>
</div>
</body>
</html>
Output :-
Total cost = 5
Example :-
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="firstName='Mahtab';lastName='Habib'">
<p>The full name is: {{ firstName + " " + lastName }}</p>
</div>
</body>
</html>
Output :-
The Full name is : Mahtab Habib
Example :-
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
<p>The full name is: <span ng-bind="firstName + ' ' + lastName"></span></p>
</div>
</body>
</html>
Output :-
AngularJS Objects
AngularJS objects are like JavaScript objects:
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="person={firstName:'mahtab',lastName:'habib'}">
<p>The name is {{ person.lastName }}</p>
</div>
</body>
</html>
AngularJS Arrays
AngularJS arrays are like JavaScript arrays:
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The third result is {{ points[2] }}</p>
</div>
</body>
</html>
Output :-
The third result is 19
File name : index.php
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The third result is <span ng-bind="points[2]"></span></p>
</div>
</body>
</html>
Output :-
The third result is 19
Example :-
File name : index.php
<html>
<head>
<title>AngularJS Expressions</title>
</head>
<body>
<h1>Sample Application</h1>
<div ng-app = "" ng-init = "quantity = 1;cost = 50; student = {firstname:'Mahtab',lastname:'alam',rollno:0231};marks = [80,90,75,73,60]">
<p>Hello {{student.firstname + " " + student.lastname}}!</p>
<p>Expense on Books : {{cost * quantity}} Rs</p>
<p>Roll No: {{student.rollno}}</p>
<p>Marks(Math): {{marks[3]}}</p>
</div>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</body>
</html>
Output :-
Sample Application
Hello Mahtab alam!
Expense on Books : 30 Rs
Roll No: 0231
Marks(Math): 73
Hello Mahtab alam!
Expense on Books : 30 Rs
Roll No: 0231
Marks(Math): 73
Example :-
File name : index.php