Can I trigger a form submit from a controller?

后端 未结 6 1485
独厮守ぢ
独厮守ぢ 2020-12-05 00:04

I\'d like to do a traditional form submit from within a controller. The scenario is that I want to hit a route on my web server and redirect to its response, which I can do

6条回答
  •  独厮守ぢ
    2020-12-05 00:49

    Expanding from @ReklatsMasters's answer, if you want to change a value before submitting the form, you could do like so...

    .directive("ngFormCommit", [function(){ return { require:"form", link: function($scope, $el, $attr, $form) { $form.commit = function($newCurrency) { $el[0].querySelector('#currency_code').value = $newCurrency; $el[0].submit(); }; } }; }]) .controller("AwesomeCtrl", ["$scope", function($scope){ $scope.save = function($newCurrency, $form) { if ($form.$valid) { $form.commit($newCurrency); } }; }])

提交回复
热议问题