我可以触发从控制器提交的表单吗? [英] Can I trigger a form submit from a controller?

查看:22
本文介绍了我可以触发从控制器提交的表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从控制器内部提交传统表单.这种情况是我想在我的网络服务器上点击一个路由并重定向到它的响应,我可以用 HTML 中的常规表单来做,但我也想在按下提交按钮时对其字段进行一些验证,并且如果验证失败,我不想做路由.

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 with a regular form in HTML, but I also want to do some validation on its fields when the submit button is pressed, and if the validation fails, I don't want to do the route.

我知道 ng-valid,但我只希望在点击按钮时进行验证.

I'm aware of ng-valid, but I only want the validation to take place when the button is hit.

有没有办法在控制器内有条件地提交表单?

Is there a way to conditionally do a form submit from within a controller?

推荐答案

您可以向 FormController 添加提交方法.我这样做了:

You can add submit method to a FormController. I did so:

<form ng-form-commit action="/" name='payForm' method="post" target="_top">
    <input type="hidden" name="currency_code" value="USD">
    <button type='button' ng-click='save(payForm)'>buy</button>
</form>

.directive("ngFormCommit", [function(){
    return {
        require:"form",
        link: function($scope, $el, $attr, $form) {
            $form.commit = function() {
                $el[0].submit();
            };
        }
    };
}])

.controller("AwesomeCtrl", ["$scope", function($scope){
   $scope.save = function($form) {
     if ($form.$valid) {
         $form.commit();
     }
   };
}])

这篇关于我可以触发从控制器提交的表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆