AngularJS: Call the ng-submit event outside the form

前端 未结 10 1469
渐次进展
渐次进展 2020-12-09 02:07

I\'m a fish in AngularJS and I have this scenario.

10条回答
  •  独厮守ぢ
    2020-12-09 02:25

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


    Easiest way for me create special directive for external submission.

    Important only use right event .triggerHandler('submit') on form element

    $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 my answer here How to trigger form submit programmatically in AngularJS?

提交回复
热议问题