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.