I’d like to submit a form but add some hidden inputs first. The hidden inputs are added with a ng-repeat. Eventually they will be rendered, but how can I be sure that the DOM is already updated prior to triggering the submit event?
partial:
<form ng-submit="addValuesAndSubmit()">
<input type="hidden" name="{{name}}" value="{{value}}" ng-repeat="(name, value) in order">
<input type="submit">
</form>
controller:
$scope.addValuesAndSubmit = function() {
$scope.order = { param1: 1, param2: 2 };
// TODO: wait until form is rendered
// there should be two <input type="hidden"> now
// trigger submit action
};
As I understand it, a post-linking function for a directive could be used for this. Am I right or am I missing something obvious?
EDIT: The form data needs to be posted to an external website, redirecting the browser to the response. It’s a payment integration where I calculate an HMAC on the server, add it to the form as a hidden element and then post it to the payment provider. The implementation of $http.post() seems not to do that redirect but returns the response instead.
With Angular apps, you often want to submit your model, rather than extract your data from form elements. I.e., use ng-model to specify $scope model object properties, then submit that model object via ng-submit. It would seem to me that you already have access to order in your $scope, since you are trying to get your associated view to use it. So you probably don't need to wait for the view to update -- just use order directly in your addValuesAndSubmit() function.