angular $eval Example – What is $eval in AngularJS?

angular $eval is a function property of the global object. The AngularJS $eval method is used to executes the AngularJS expression on the current scope and returns the result.

angular $eval – AngularJS $scope.$eval() Function

AngularJS’s $parse service processes these expressions. $eval and $parse don’t evaluate JavaScript;

Syntax of angular $eval

$eval([expression], [locals]);

AngularJS $scope.$eval() Function Example

index.html

<!DOCTYPE html>        
<html>        
<head> <!-- www.pakainfo.com -->        
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>         
    <meta charset="utf-8">        
    <title>AngularJS $eval Example - www.pakainfo.com </title>        
  <script>        
var app = angular.module('app', []);        
     app.controller("PakainfoContrl", function($scope) { 
       $scope.firstNo = 58;
       $scope.secondNo = 47;
       $scope.output = $scope.$eval('firstNo +secondNo');   
    });     
</script>        
</head>        
<body style="background-color:#3d3d3d;">        
  <fieldset style="background-color:#3d3d3d;">                    
    <legend>AngulerJS $eval Method Example - pakainfo.com</legend>         
  <div ng-app="app">              
    <div ng-controller="PakainfoContrl">         
      <input type="text" ng-model="firstNo" placeholder="Enter Number">  + <input type="text" ng-model="secondNo" placeholder="Enter Number">
<p style="font-family:Arial;color:green;background:red;padding:4px;width:450px;"    
     >{{'Result :- ' + output}}</p>   
    </div>              
</div>        
 </fieldset>         
</body>        
</html>

Don’t Miss : AngularJS Expressions Tutorial With Example

Read Also:  ng-mouseover Event using Angular Example

Example : Eval function in angular

<!DOCTYPE html>
<html>
<head>
    <meta chrset="UTF 8">
    <title>Event Registration - www.pakainfo.com</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="demoApplication" ng-controller="AngularController">
    <h2> Pakainfo.com Global Event</h2>
    {{value}}
</div>
<script>
    var demoApplication = angular.module('demoApplication',[]);
    demoApplication.controller('AngularController',function($scope){
        $scope.a=98;
        $scope.b=2;

        $scope.value=$scope.$eval('a+b');
    });
</script>

</body>
</html>

This is an example of using $eval in angularjs to call javascript function when their name is given as a string.