ng submit - how to update form hidden data before submssion in AngularJS? -


this question has answer here:

i have model variables bound hidden input fields in form.

when user hits submit button, try update these fields updating corresponding bound model variables not change (i noticed when dumping request on server, receive old data), noticed every thing works o.k when use normal text inputs rather hidden inputs

here code:

form

<form name="bla bla" action="bla bla" method="post" ng-submit="updateform()">    <input type="hidden" name="token" ng-model= "extra.token" />   <input type="hidden" name="filters" ng-model="extra.filters" />   <button type="submit">     submit form   </button> </form> 

controller

var app = angular.module(... // bla bla  app.controller('mycontroller', ['$scope', ctrldef]);  function ctrldef($scope) {   $scope.extra = {};   $scope.extra.token = '';   $scope.extra.filters = '';    $scope.updateform = function() {       $scope.extra.token = 'test1';       $scope.extra.filters = 'test2';   }; } 

i don't think ngsubmit reliably run before post request if use standard html form attributes (method, action, etc). better idea use $html service send request. has benefit of being asynchronous, shouldn't need hidden iframe @ moment.

app.controller('mycontroller', ['$scope', '$html', ctrldef]);  function ctrldef($scope, $html) {   $scope.extra = {};   $scope.extra.token = '';   $scope.extra.filters = '';    $scope.updateform = function() {       $scope.extra.token = 'test1';       $scope.extra.filters = 'test2';        $http.post(extra, "your post url here").then(function (response) {           // stuff response here...       });   }; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -