问题
So i'm using Stripe to accept payment on a form that also takes other info - name - address etc. I'm using the following library:
https://github.com/gtramontina/stripe-angular
and I have the form set up like this:
<form id="signup-form" ng-submit="processForm()" stripe:form="saveCustomer">
<!-- our nested state views will be injected here -->
<div id="form-views" ui-view></div>
</form>
and an example of nested form view (non stripe stuff)
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" ng-model="formData.name" required>
</div>
and the stripe stuff:
<div class="form-group">
<label for="card_number">Card Number</label>
<input type="text" class="form-control" size="20" data-stripe="number" ng-model="formData.card"/>
</div>
my controller (only relevant function):
// function to process the form
$scope.saveCustomer = function(status, response) {
var $form = $('#signup-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
var html = '<input type="hidden" name="stripeToken" ng-model=formData.'+token+'/>';
// Insert the token into the form so it gets submitted to the server
$form.append($(html));
// and submit
$form.get(0).submit();
console.log(formData);
}
}
Here is my question - if the 'saveCustomer' function is the equivalent of the stripeResponseHandler function in the stripe docs, then they are submitting the form after appending the token to it. If i'm adding data into my formData object, how do I add this information into that object. My logic now is that I'm adding the ng-submit attribute linking to another function (processForm) in the controller but this seems rubbish and I can't get the data anyway - How can i get the token and all the other formData data into a single function to send it to the server if I can't see the formData within the saveCustomer function?
回答1:
It dones not match Angular-way to treat DOM directly through using jquery. I recommend you to use model object instead of treating DOM. In your sample code, model object is 'formData'.
If you want to access $scope.formData, the code like below help you to do so.
$scope.formData = {};
var formData = $scope.formData;
$scope.saveCustomer = function(status, response) {
console.log(formData); // NOT $scope.formData;
}
来源:https://stackoverflow.com/questions/29134086/how-do-i-send-additional-user-information-in-a-form-after-getting-stripe-token