What is the use of $eval in alert()?

0 votes
I want to know what's the use of $eval in alert function instead we can simply evaluate using $scope variable in AngularJS
Feb 4 in Angular by kartik
• 27,140 points
109 views

1 answer to this question.

0 votes

Hey, kartik!!

There is not much difference in using $eval in alert and not using it.

Let us understand with an example.

Consider the following code:

var app=angular.module('app',[])

app.controller('sample',['$scope',function($scope){

$scope.a=10;

$scope.b=20;

$scope.demo=function(){

alert("result:" +($scope.a* $scope.b));//(Angular Expression)

}

}]);

This will evaluate the expression ($scope.a* $scope.b) as a and b are in the scope.

However now consider the following code

var app=angular.module('app',[])

app.controller('sample',['$scope',function($scope){

$scope.a=10;

$scope.b=20;

$scope.demo=function(){

alert("result:" +(a*b));//(Javascript Expression)

}

}]);

Here,the expression (a*b) give an error because it can't determine the scope of 'a' as well as 'b'.

so now the problem arise what to do to evaluate both Angularjs expression as well as Javascript expression?

To solve this there is a method called $eval which is made available through $scope.

so the solution code  can be:

var app=angular.module('app',[])

app.controller('sample',['$scope',function($scope){

$scope.a=10;

$scope.b=20;

$scope.demo=function(){

alert("result:" +$scope.$eval("a*b"));

}

}]);

During run time the expression $scope.$eval("a*b") is evaluated as ($scope.a* $scope.b) internally and (a*b) is consider as string that passes to $eval.

answered Feb 4 by Niroj
• 58,500 points

Related Questions In Angular

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

What is the service that is used to configure the UI-Router and ngRoute in AngularJs?

Routing comes into play whenever you want ...READ MORE

answered Feb 11 in Angular by anonymous
• 58,500 points
45 views
0 votes
1 answer

What is the top advantages of using UI-router over ngRoute?

Hey, In Client side Routing there are several ...READ MORE

answered Feb 11 in Angular by anonymous
• 58,500 points
135 views
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17 in Web Development by anonymous
• 27,140 points
85 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20 in Web Development by Niroj
• 58,500 points

edited Jan 21 by Niroj 124 views
+1 vote
1 answer

What are pseudo class in css??

Hey, The state of an element is controlled  by ...READ MORE

answered Jan 20 in Web Development by Niroj
• 58,500 points

edited Jan 21 by Niroj 70 views
0 votes
1 answer

What is $watch() and it's use in AngularJS?

$watch() function is used to watch the ...READ MORE

answered Feb 3 in Angular by anonymous
• 58,500 points
124 views
0 votes
1 answer