In Focus

Expressions In AngularJS

In this article you will learn about Expression in AngularJS.

Introduction

Expressions in AngularJS are just like JavaScript code snippets. JavaScript code is usually written inside double braces: {{expression}}. In other words, Angular Expression are JavaScript code snippets with limited sub-set. Expressions are included in the HTML elements.

Example:

  1. <div ng-controller="appController">  
  2.     <span>  
  3.         4+5 = {{4+5}}  
  4.     </span>  
  5.     <br />  
  6.     <br />  
  7.     <span ng-init="quantity=5;cost=25">  
  8.         Total Cost = {{quantity * cost}}  
  9.     </span>  
  10. </div>  
Output:

Expression example

Angular Expressions vs. JavaScript Expressions

The following are the differences between Angular Expressions and JavaScript Expressions

 

  1. Context

    JavaScript expressions are evaluated globally whereas Angular expressions are evaluated locally using scope object. AngularJS does not use JavaScript function "EVAL" to evaluate expressions. AngularJS has one own service called "$parse" to evaluate expressions. AngularJS expression does not has access to the global variables such as document, location, etc. This restriction prevents accidental access to the global state. To access window and location global variables, AngularJS provided services $window and $location.

    Example:

    In the following example, I added two button and write click event by using directive ng-click. First button call controller function and alert some text. Second button directly call alert global function. Result first button works fine but second function does not work due to restriction of AngularJS.

    HTML:
    1. <div ng-controller="appController">  
    2.     <div>  
    3.         <h4>Context Understanding Example</h4>  
    4.         <span>  
    5.             <button ng-click="showAlert()">Click Me 1</button>  
    6.             <button ng-click="alert('It does not work!')">Click Me 2</button>  
    7.         </span>  
    8.     </div>  
    9. </div>  
    Controller:
    1. <script>  
    2.     var app = angular.module("myapp", []);  
    3.     app.controller("appController", ['$scope''$window', function ($scope, $window) {  
    4.         $scope.showAlert = function () {  
    5.             $window.alert('Hello C# Corner');  
    6.         }  
    7.     }]);  
    8. </script>  
    Output:

    message

  2. Cannot use control flow statement

    We cannot write any control flow statement in AngularJS expression. But we can use ternary operator in expression. One of the reason of this behavior of AngularJS is we have to write all application logic inside the controller not in view. It means that we cannot use any conditional statement, loop and exception handling statement with expression.

  3. We cannot declare function in AngularJS expression. This is due to avoid complex model transformation logic inside templates.

  4. We cannot use or create regular expressions in an Angular expression.

  5. We cannot use comma (,) or "void" operator in an AngularJS expression.

  6. JavaScript expression generates type error and reference error when trying to evaluate undefined properties whereas AngularJS expression does not generate any exception.

  7. We can use filter within the AngularJS expression to format and filter data before displaying it.

One time binding

An expression that starts with double colon (::) is considered as a one-time expression. This expression will stop recalculating expressions when they are stable. It happens after the first digest if expression value(s) is not an  undefined value.

Example:

In following example, I have put two span and defining expression: one with double colon (::) and second without double colon (::). And I am incrementing the value of expression on button click. Result is after the click of button, the  first span shows value 0 (initial value) and second span shows value 3 (current value).

  1. <div ng-controller="appController">  
  2.     <div>  
  3.         <h4>One-Time Binding Example</h4>  
  4.         <span>  
  5.             <button ng-click="showCurrentValue()">Click Me</button>  
  6.             <br />  
  7.             <span>One-time binding: Button click count: {{::currentCount}}</span>  
  8.             <br />  
  9.             <span>Normal binding: Button click count: {{currentCount}}</span>  
  10.         </span>  
  11.     </div>  
  12. </div>  
  13.   
  14. <script>  
  15.     var app = angular.module("myapp", []);  
  16.     app.controller("appController", ['$scope''$window', function ($scope, $window) {  
  17.         $scope.currentCount = 0;  
  18.         $scope.showCurrentValue = function () {  
  19.             $scope.currentCount++;  
  20.         }  
  21.     }]);  
  22. </script>  
Output:

Output

Please note that one time binding feature introduced in AngularJS version 1.3.

Summary

Expression is a very useful feature of AngularJS. It binds the data to the HTML. AngularJS expressions are mostly same as JavaScript expressions and they can have literals, operators, and variables.