Find the source code below:
https://github.com/jvdvleuten/angular-button-spinnerThis is a simple directive (765 bytes when minified) to add a spinner while loading some data for user feedback. It uses font-awesome to render the spinner by default, but you can customize all classes as can be seen below.
It can be used on every element (e.g. <a>, <button>), as an attribute.
Best results are obtained when used in combination with font-awesome and bootstrap. I recommend using it in combination with ng-disable to prevent firing multiple requests.
<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading">Spin me, try me out now!</a>
Spin me, try me out now! Such wow, many spin, much awesome
Look below for more examples
bower install angular-button-spinner --save
angular.module('MyApp', ['angular-button-spinner']);
In order to use the default icon, or the fa-icons in the examples, please install font-awesome as well.
button-spinner="loading" (required)Base attribute for the directive to work
loading: scope variable that toggles the spinnerspinner-icon="classnames"Customize the spinning icon
loading: classname(s) seperated with a space (e.g.: "fa fa-spinner fa-spin")button-append="classnames"Appends an icon when not spinning
classnames: classname(s) seperated with a space (e.g.: "fa fa-chevron-right")button-prepend="classnames"Prepends an icon when not spinning.
Note: When spinning, the spinning icon will be in place of the prepend icon.
<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading">Save</a>
Save
<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading" ng-disabled="loading">Save</a>
Save
<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading" button-append="fa fa-chevron-right">Next</a>
Next
<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading" button-prepend="fa fa-save">Save</a>
Save
<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading" spinning-icon="fa fa-circle-o-notch fa-spin">Save</a>
Save<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading" spinning-icon="fa fa-home fa-spin" button-append="fa fa-chevron-right">Next</a>
Next<a class="btn btn-success" ng-click="toggleLoading()" button-spinner="loading" spinning-icon="fa fa-save fa-spin" button-prepend="fa fa-save">Save</a>
Save
The example controller controls the loading variable in the scope. It resets after 2 seconds.
angular.module('MyApp').controller('MyController', ['$scope','$timeout',
function ($scope, $timeout) {
$scope.loading = false;
$scope.toggleLoading = function() {
$scope.loading = true;
$timeout(function() {
$scope.loading = false;
}, 2000);
};
}
]);