I have a function in a service that takes a long while to calculate so I would like to show a message on the scope to show it's calculating. I have created a (simplified) jsfiddle here: http://jsfiddle.net/rikboeykens/brwfw3g9/
var app = angular.module('myApp', []);
app.service('testService', function(){
this.longFunction=function(){
for (var i=0; i<1000000000;i++){
}
}
});
function TestCtrl($scope, testService)
{
$scope.$apply(function(){$scope.waiting="not waiting";});
$scope.longFunction = function(){
$scope.waiting="waiting";
testService.longFunction();
$scope.waiting="not waiting";
}
}
The longFunction on the testService takes about two seconds to complete, but $scope.waiting is not updated to "waiting" while this is taking place.
I tried using $scope.$apply but then I just get an error saying $apply is already in progress.
Would it work if I perform the longFunction on the testService asynchronously? I have been looking into using promises but I'm not sure how I would go about implementing them.