Download Angular Spinner by http request
I am trying to use Angular Spin Kit in my project to load a spinner for each http GET, PUT, POST and DELETE method. Here's a fiddle for the Angular Spin Kit.
<circle-spinner></circle-spinner>
How can I connect this counter with my controller code so that when the http call is called I can load this counter in my view. thank
source to share
you can use a variable $scope
to detect the progress of the ajax request. based on this variable you can show or hide the spinner
$scope.sendAjax = function() {
$scope.prograssing = true; // show spinner
$http.get('data.json').then(function() {
//sucess
$scope.prograssing = false; // hide spinner when success ajax
} , function() {
//error
$scope.prograssing = false; // hide spinner when unsuccessful ajax
});
}
source to share
First of all you need to follow these instructions on how to install angular-spinkit in your project.
Second, see this Plnkr for how I managed to use angular spinkit for GET calls.
These are the main steps:
-
Insert your directive
<circle-spinner></circle-spinner>
into the code, but make it invisible with theng-show
following:<circle-spinner ng-show="showSpinner"></circle-spinner>
showSpinner
is the $ scope variable that we introduce asfalse
, since we don't want the spinning circle to be shown until we make a call$http
. -
Make your GET call using angular service
$http
. Use a function for this. When you call a function, the first thing to do is set the variable$scope.showSpinner
totrue
so that your counter is visible. Then you call the call$http
. -
The next step is to process the GET response. When the call is complete (you have success or error parameters), you must tell the variable
$scope.showSpinner
to false, which means that the directiveng-show
from the directive<circle-spinner>
will become false so that the circle is no longer displayed.
In short, you are showing an invisible spinning circle, you are making a call $http
that makes it visible; when the response is returned, the circle is no longer visible.
source to share