Skip to content

Instantly share code, notes, and snippets.

@weberste
Last active August 29, 2015 14:01
Show Gist options
  • Save weberste/be32c7301576259e9f96 to your computer and use it in GitHub Desktop.
Save weberste/be32c7301576259e9f96 to your computer and use it in GitHub Desktop.
Spinning wheel on button click
app.directive('clickSpinner', ['$q', function ($q) {
var spinnerId = 1;
var directive = {
restrict: 'A',
link: link,
transclude: true,
scope: {
clickHandler: '&clickSpinner'
},
template: '<span style="position: relative"><span ng-transclude></span></span>'
};
var opts = {
width: 3,
length: 3,
lines: 9,
radius: 4,
color: '#C9D1FF'
};
return directive;
function link(scope, element, attr) {
var spinner = new Spinner(opts);
var spinnerTarget = element.children();
var textElement = spinnerTarget.children();
function handler() {
var p = $q.when(scope.clickHandler());
attr.$set('disabled', true);
textElement.css('visibility', 'hidden');
spinner.spin(spinnerTarget[0]);
p['finally'](function() {
attr.$set('disabled', false);
textElement.css('visibility', 'visible');
spinner.stop();
});
}
element.on('click', handler);
scope.$on('$destroy', function() {
element.off('click', handler);
});
}
}]);
<!-- if saveClient() returns a promise, the button text is
replaced with a spinner until the promise is resolved -->
<button click-spinner="saveClient(client, clientForm)">
Submit
</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment