我可以触发形式从控制器提交? [英] Can I trigger a form submit from a controller?

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

问题描述

我想做一个传统的形式从控制器内提交。这种情况的出现,我想打我的Web服务器上的路线并重定向到它的反应,我可以在HTML常规形式做的,但我也想要做某个字段时提交按钮一些验证是pressed,如果验证失败,我不想做的路线。

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-有效的,但我只想当按钮被击中验证的发生。

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天全站免登陆