Expressions are variables which were defined in the double braces {{ }}. They are very commonly used within Angular JS, and you would see them in our previous tutorials.
In this tutorial, you will learn-
AngularJS expressions are those that are written inside double braces {{expression}}.
Syntax:
A simple example of an expression is {{5 + 6}}.
Angular.JS expressions are used to bind data to HTML the same way as the ng-bind directive. AngularJS displays the data exactly at the place where the expression is placed.
Let's look at an example of Angular.JS expressions.
In this example, we just want to show a simple addition of numbers as an expression.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <h1> Guru99 Global Event</h1> <div ng-app=""> Addition : {{6+9}} </div> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
Expressions can be used to work with numbers as well. Let's look at an example of Angular.JS expressions with numbers.
In this example, we just want to show a simple multiplication of 2 number variables called margin and profit and displayed their multiplied value.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <h1> Guru99 Global Event</h1> <div ng-app="" ng-init="margin=2;profit=200"> Total profit margin {{margin*profit}} </div> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
Expressions can be used to work with strings as well. Let's look at an example of Angular JS expressions with strings.
In this example, we are going to define 2 strings of "firstName" and "lastName" and display them using expressions accordingly.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <h1> Guru99 Global Event</h1> <div ng-app="" ng-init="firstName='Guru';lastName='99'"> First Name : {{firstName}}<br> last Name : {{lastName}} </div> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output, it can be clearly seen that the values of firstName and lastName are displayed on the screen.
Expressions can be used to work with JavaScript objects as well.
Let's look at an example of Angular.JS expressions with javascript objects. A javascript object consists of a name-value pair.
Below is an example of the syntax of a javascript object.
Syntax:
var car = {type:"Ford", model:"Explorer", color:"White"};
In this example, we are going to define one object as a person object which will have 2 key value pairs of "firstName" and "lastName".
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <h1> Guru99 Global Event</h1> <div ng-app="" ng-init="person={firstName:'Guru',lastName:'99'}"> First Name : {{person.firstName}}<br> Last Name : {{person.lastName}} </div> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
Expressions can be used to work with arrays as well. Let's look at an example of Angular JS expressions with arrays.
In this example, we are going to define an array which is going to hold the marks of a student in 3 subjects. In the view, we will display the value of these marks accordingly.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <h1> Guru99 Global Event</h1> <div ng-app="" ng-init="marks=[1,15,19]"> Student Marks<br> Subject1 : {{marks[0] }}<br> Subject2 : {{marks[1] }}<br> Subject3 : {{marks[2] }}<br> </div> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output, it can be clearly seen that the marks being displayed, that are defined in the array.
Angular.JS Expression capabilities
Angular JS Expression limitations
The $eval function allows one to evaluate expressions from within the controller itself. So while expressions are used for evaluation in the view, the $eval is used in the controller function.
Let's look at a simple example on this.
In this example,
We are just going to use the $eval function to add 2 numbers and make it available in the scope object so that it can be shown in the view.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <div ng-app="sampleApp" ng-controller="AngularController"> <h1> Guru99 Global Event</h1> {{value}} </div> <script> var sampleApp = angular.module('sampleApp',[]); sampleApp.controller('AngularController',function($scope){ $scope.a=1; $scope.b=1; $scope.value=$scope.$eval('a+b'); }); </script> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
The above output shows the output of the expression which was evaluated in the controller. The above results show that the $eval expression was used to perform the addition of the 2 scope variables 'a and b' with the result sent and displayed in the view.
Summary: