Skip to content

Instantly share code, notes, and snippets.

@pc035860
Created September 7, 2013 02:24
Show Gist options
  • Save pc035860/6472267 to your computer and use it in GitHub Desktop.
Save pc035860/6472267 to your computer and use it in GitHub Desktop.
AngularJS submitIt module
angular.module('submitIt', [])
/**
* @ngdoc directive
* @description form submission trigger
*
* @param {boolean} submit-it submit form on `true`
* @param {string} si-action submit action(optional)
* @param {string} si-method submit method(optional)
*/
.directive('submitIt', [
'$timeout',
function ($timeout) {
return {
restrict: 'A',
link: function postLink(scope, iElm, iAttrs) {
var _submitWatch = scope.$watch(iAttrs.submitIt, _onSubmitWatch),
_savedAction, _savedMethod;
iAttrs.$observe('siAction', function (val) {
if (val) {
_savedAction = val;
}
});
iAttrs.$observe('siMethod', function (val) {
if (val) {
_savedMethod = val;
}
});
function _onSubmitWatch(submit) {
if (submit && iElm[0].tagName === 'FORM') {
if (_savedAction) {
iElm.attr('action', _savedAction);
}
if (_savedMethod) {
iElm.attr('method', _savedMethod);
}
$timeout(function () {
iElm[0].submit();
});
_submitWatch();
}
}
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment