I have a page where a user enters initial data, after that he clicks Calculate. And on click there should appear a gif-image with a loading spinner while the calculations are being made. As soon as the result is ready, the gif-image should disappear and the result should be displayed on the page. Here's the code I'm using:
$scope.loading=false; //initially the img is invisible
$scope.calc=function(){
$scope.loading=true;//make the img visible
var result=MyService.calc(input_data); //processing data
$scope.result=result;
$scope.loading=false;//hide the img
}
And the HTML image is defined like so:
<div class="span1">
<div ng-if="loading"><img ng-src="img/ajax-loader.gif"></div>
</div>
It's an expected order of things to happen. But in reality it works as follows: after clicking Calculate the image doesn't appear, and after some time during which calculations were made the image is shown and escaped.
The calculations are made in a service.
What's the problem? I already tried to make a $q.defer()
in the service which resolves with result. And then in the controller display result inside the promise.then
function. But it doesn't work.