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:
- <div ng-controller="appController">
- <span>
- 4+5 = {{4+5}}
- </span>
- <br />
- <br />
- <span ng-init="quantity=5;cost=25">
- Total Cost = {{quantity * cost}}
- </span>
- </div>

Angular Expressions vs. JavaScript Expressions
The following are the differences between Angular Expressions and JavaScript Expressions
- 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:
Controller:- <div ng-controller="appController">
- <div>
- <h4>Context Understanding Example</h4>
- <span>
- <button ng-click="showAlert()">Click Me 1</button>
- <button ng-click="alert('It does not work!')">Click Me 2</button>
- </span>
- </div>
- </div>
Output:- <script>
- var app = angular.module("myapp", []);
- app.controller("appController", ['$scope', '$window', function ($scope, $window) {
- $scope.showAlert = function () {
- $window.alert('Hello C# Corner');
- }
- }]);
- </script>

- 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.
- We cannot declare function in AngularJS expression. This is due to avoid complex model transformation logic inside templates.
- We cannot use or create regular expressions in an Angular expression.
- We cannot use comma (,) or "void" operator in an AngularJS expression.
- JavaScript expression generates type error and reference error when trying to evaluate undefined properties whereas AngularJS expression does not generate any exception.
- We can use filter within the AngularJS expression to format and filter data before displaying it.


Santhakumar MunuswamyPosted Oct 18, 2015, 5:38 AM
Good one
Muhammad Aqib ShehzadPosted Oct 7, 2015, 3:19 PM
nice dear
Harshad PansuriyaPosted Oct 7, 2015, 7:22 AM
Nice one
Nilesh JadavPosted Oct 7, 2015, 5:48 AM
Good one