I'm using an input mask directive to force an input field to adjust to a fixed decimal format (dependent on the locale) in AngularJS 1.4.9.
This is my code:
<div ng-app="MyApp">
<div ng-controller="MyCtrl">
<form action="http://httpbin.org/post" method="post" name='myForm'>
<input type="text" name="amount" ui-number-mask="2" ng-model="debit.amount">
<input type='submit'>
</form>
</div>
</div>
Everything works as expected. The model holds the non formatted decimal value and the input field displays the formatted value.
The problem arises when I hit submit and the form is submitted in a traditional POST fashion.
The server receives the following:
{
"form": {
"amount": "1,299.99",
},
}
instead of
{
"form": {
"amount": "1299.99",
},
}
I believe it's because it submits $viewValue
instead of $modelValue
.
Workarounds I've tried
Use a hidden field and set the
VALUE
attribute to the model value:<input type='hidden' name="amount_hidden" value="{{debit.amount}}">
It works, but it's a bit of a hassle having to duplicate fields. See JsFiddle.
Submit using AngularJS'
$http.post()
to mimic a traditional form post. This also works, but it's even more cumbersome than the previous. See JsFiddle.
Is there an easier way to force the submission of $modelValue
?