Created
January 25, 2017 20:15
-
-
Save wholypantalones/07e77bec50bacf1faf8bea34e1ea1d5a to your computer and use it in GitHub Desktop.
Submit a form with angular directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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