How to use Angular.js Expressions,Numbers,Strings,Objects and Arrays
In Angular.js all Numbers,Strings,Object and Arrays are exactly similar to java script.
Angular.js Expressions
AngularJS expressions are written inside double braces: {{ expression }}.
AngularJS expressions binds data to HTML the same way as the ng-bind directive.
AngularJS will "output" data exactly where the expression is written.
AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and variables.
In each section i entered examples for both {{ expression }} and ng-bing.
AngularJS Numbers
AngularJS numbers are like JavaScript numbers:
Example Using expression:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total in dollar: {{ quantity * cost }}</p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output:
Total in dollar : 5
Same example using ng-bind:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total in dollar: <span ng-bind="quantity * cost"></span></p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
OutPut:
Total in dollar:5
AngularJS Strings
AngularJS strings are like JavaScript strings:
Example Using expression:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="firstName='manish';lastName='tiwari'">
<p>The full name is: {{ firstName + " " + lastName }}</p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output :
The full name is: manish tiwari
Same example using ng-bind:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="firstName='manish';lastName='tiwari'">
<p>The full name is: <span ng-bind="firstName + ' ' + lastName"></span></p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output :
The full name is: manish tiwari
AngularJS Objects
AngularJS objects are like JavaScript objects:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="person={firstName:'manish',lastName:'tiwari'}">
<p>The name is {{ person.lastName }}</p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output:
The name is tiwari
Same example using ng-bind:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="person={firstName:'manish',lastName:'tiwari'}">
<p>The name is <span ng-bind="person.lastName"></span></p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output:
The name is tiwari
AngularJS Arrays
AngularJS arrays are like JavaScript arrays:
Example Using Expressions:
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The third result is {{ points[2] }}</p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output:
The third result is 19
Same example using ng-bind:
<!DOCTYPE html>
<html>
<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>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Output:
The third result is 19