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, 2020 in Angular by kartik
• 37,510 points
976 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, 2020 by Niroj
• 82,880 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, 2020 in Angular by anonymous
• 82,880 points
392 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, 2020 in Angular by anonymous
• 82,880 points
971 views
0 votes
1 answer
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, 2020 in Web Development by anonymous
• 37,510 points
748 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, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 803 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, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 682 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, 2020 in Angular by anonymous
• 82,880 points
2,145 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP