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
• 1,910 points
20 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
• 3,230 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 6 days ago in Angular by anonymous
• 3,230 points
12 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 6 days ago in Angular by anonymous
• 3,230 points
15 views
0 votes
0 answers
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
• 1,910 points
26 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
• 3,230 points

edited Jan 21 by Niroj 29 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
• 3,230 points

edited Jan 21 by Niroj 22 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
• 3,230 points
31 views
0 votes
1 answer