如何在AngularJS中以编程方式触发表单提交?

时间:2022-11-24 19:43:42

From Angular's documentation it follows that ngSubmit is a preferred way of submitting a form. All pending operations are immediately finished and $submitted flag is set as well. Whereas listening to ngClick event does not have the same effect.

从Angular的文档中可以看出,ngSubmit是提交表单的首选方式。所有待处理的操作立即完成,并且还设置了$ submitted标志。而听取ngClick事件却没有同样的效果。

Now I need to submit a form with hotkeys having all the goodies of ngSumbit approach. Hence I need some way to trigger standard submit workflow.

现在我需要提交一个表格,其中包含具有ngSumbit方法所有优点的热键。因此,我需要一些方法来触发标准提交工作流程。

I tried submit() on DOM form and it worked, but angular's form object attached to scope contains no references to DOM form, thus I need to access it through jqLite:

我在DOM表单上尝试了submit()并且它有效,但是附加到scope的angular的form对象不包含对DOM表单的引用,因此我需要通过jqLit​​e访问它:

var frm = angular.element('#frmId');
frm.submit();

I didn't like this solution for accessing DOM in controller code so I created a directive:

我不喜欢这个在控制器代码中访问DOM的解决方案,所以我创建了一个指令:

function wuSubmit() {
    return {
        require: 'form',
        restrict: 'A',
        link: function(scope, element, attributes) {
            var scope = element.scope();
            if (attributes.name && scope[attributes.name]) {
                scope[attributes.name].$submit = function() {
                    element[0].submit();
                };
            }
        }
    };
}

which extends form object with $submit method.

使用$ submit方法扩展表单对象。

Both variants do not work for yet unknown reason. form.submit() tries to send data, default action is not prevented.


Update 1
It turns out that Angular listens to click events of elements having type="input".
Eventually I decided to trigger click event for that element:

两种变体都不能用于未知原因。 form.submit()尝试发送数据,不会阻止默认操作。更新1事实证明,Angular会侦听具有type =“input”的元素的click事件。最终我决定触发该元素的click事件:

wuSubmit.$inject = ['$timeout'];
function wuSubmit($timeout) {
    return {
        require: 'form',
        restrict: 'A',
        link: function (scope, element, attributes) {
            var submitElement = element.find('[type="submit"]').first();

            if (attributes.name && scope[attributes.name]) {

                scope[attributes.name].$submit = submit;
            }

            function submit() {
                $timeout(function() {
                    submitElement.trigger('click');
                });
            }
        }
    };
}

Is there any out of the box solution for this functionality?

这个功能有没有开箱即用的解决方案?

3 个解决方案

#1


4  

Just use event .triggerHandler('submit') on form element.

只需在表单元素上使用事件.triggerHandler('submit')。

myApp.directive("extSubmit", ['$timeout',function($timeout){
    return {
        link: function($scope, $el, $attr) {
            $scope.$on('makeSubmit', function(event, data){
              if(data.formName === $attr.name) {
                $timeout(function() {
                  $el.triggerHandler('submit'); //<<< This is Important

                  //equivalent with native event
                  //$el[0].dispatchEvent(new Event('submit')) 
                }, 0, false);   
              }
            })
        }
    };
}]);

Look at http://jsfiddle.net/84bodm5p/

看看http://jsfiddle.net/84bodm5p/

#2


0  

You can modify your directive code a bit like:

你可以修改你的指令代码:

function wuSubmit() {
    return {
        require: 'form',
        restrict: 'A',
        link: function(scope, element, attributes) {
            var scope = element.scope();
            if (attributes.name && scope[attributes.name]) {
                scope[attributes.name].$submit = function() {
                    // Parse the handler of submit & execute that.
                    var fn = $parse(attr.ngSubmit);
                    $scope.$apply(function() {
                        fn($scope, {$event: e});
                    });
                };
            }
        }
    };
}

Here you don't have to add wu-submit everywhere. ng-submit will work.

在这里你不必在任何地方添加wu-submit。 ng-submit将有效。

Hope this helps!

希望这可以帮助!

#3


0  

angular.module('myapp.directive').directive("form", ['$parse', function($parse){
     return {
        require: 'form',
        restrict: 'E',
        link: function(scope, elem, attrs, form) {
            form.doSubmit = function() {
                form.$setSubmitted();
                return scope.$eval(attrs.ngSubmit);
            };
        }
    };
}]);

Html:

HTML:

<form name="myForm" ng-submit="$ctrl.submit()" novalidate>

Then call in controller

然后打电话给控制器

$scope.myForm.doSubmit();

#1


4  

Just use event .triggerHandler('submit') on form element.

只需在表单元素上使用事件.triggerHandler('submit')。

myApp.directive("extSubmit", ['$timeout',function($timeout){
    return {
        link: function($scope, $el, $attr) {
            $scope.$on('makeSubmit', function(event, data){
              if(data.formName === $attr.name) {
                $timeout(function() {
                  $el.triggerHandler('submit'); //<<< This is Important

                  //equivalent with native event
                  //$el[0].dispatchEvent(new Event('submit')) 
                }, 0, false);   
              }
            })
        }
    };
}]);

Look at http://jsfiddle.net/84bodm5p/

看看http://jsfiddle.net/84bodm5p/

#2


0  

You can modify your directive code a bit like:

你可以修改你的指令代码:

function wuSubmit() {
    return {
        require: 'form',
        restrict: 'A',
        link: function(scope, element, attributes) {
            var scope = element.scope();
            if (attributes.name && scope[attributes.name]) {
                scope[attributes.name].$submit = function() {
                    // Parse the handler of submit & execute that.
                    var fn = $parse(attr.ngSubmit);
                    $scope.$apply(function() {
                        fn($scope, {$event: e});
                    });
                };
            }
        }
    };
}

Here you don't have to add wu-submit everywhere. ng-submit will work.

在这里你不必在任何地方添加wu-submit。 ng-submit将有效。

Hope this helps!

希望这可以帮助!

#3


0  

angular.module('myapp.directive').directive("form", ['$parse', function($parse){
     return {
        require: 'form',
        restrict: 'E',
        link: function(scope, elem, attrs, form) {
            form.doSubmit = function() {
                form.$setSubmitted();
                return scope.$eval(attrs.ngSubmit);
            };
        }
    };
}]);

Html:

HTML:

<form name="myForm" ng-submit="$ctrl.submit()" novalidate>

Then call in controller

然后打电话给控制器

$scope.myForm.doSubmit();