Skip to content

Instantly share code, notes, and snippets.

@ejholmes
Created December 28, 2013 04:17
Show Gist options
  • Save ejholmes/8156063 to your computer and use it in GitHub Desktop.
Save ejholmes/8156063 to your computer and use it in GitHub Desktop.
ngSubmitLoading
(function(angular) {
'use strict';
var module = angular.module('ngSubmitLoading', []);
/**
* A directive that adds a `$loading` attribute to the ngForm controller
* until the promise is fullfilled.
*
* @example
*
* <form name="form" ng-submit-loading="fnThatReturnsPromise()">
* <button ng-disabled="form.$loading">
* Submit
* </button>
* </form>
*/
module.directive('ngSubmitLoading', function($parse) {
return {
require: '^form',
compile: function($element, attr) {
var fn = $parse(attr.ngSubmitLoading);
return function(scope, element, attr, ctrl) {
element.on('submit', function(event) {
scope.$apply(function() {
ctrl.$loading = true;
fn(scope, { $event: event }).finally(function() {
ctrl.$loading = false;
});
});
});
};
}
};
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment