Skip to content

Instantly share code, notes, and snippets.

@wholypantalones
Created January 25, 2017 20:15
Show Gist options
  • Save wholypantalones/07e77bec50bacf1faf8bea34e1ea1d5a to your computer and use it in GitHub Desktop.
Save wholypantalones/07e77bec50bacf1faf8bea34e1ea1d5a to your computer and use it in GitHub Desktop.
Submit a form with angular directive
// usage:
// <form ng-form-commit action="/" name='payForm' method="post" target="_top">
// <input type="hidden" name="currency_code" value="USD">
// <button type='button' ng-click='save(payForm)'>buy</button>
// </form>
.directive("ngFormCommit", [function(){
return {
require:"form",
link: function($scope, $el, $attr, $form) {
$form.commit = function() {
$el[0].submit();
};
}
};
}]).controller("AwesomeCtrl", ["$scope", function($scope){
$scope.save = function($form) {
if ($form.$valid) {
$form.commit();
}
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment