2

I'm trying to write a form which has ngsSubmit. Pressing Enter and clicking the submit button works as expected (calling the method I provided for ng-submit and that's that).

However, I also want in some cases to do some preprocessing before actually submitting, so I tried using JQuery's submit() to programmatically trigger the submit.

This causes the ng-submit handler to fire, AND also sends the form normally refreshing the whole page.

Any ideas how to get around this or why it happens???

Example here ("click me" shows the bad behavior) http://jsfiddle.net/Yf5tf/

<form ng-app="myApp" ng-submit="submitMe()" ng-controller="myCtrl">
    <input type="text" ng-model="value"></input>
    <input type="submit" value="Go!"></input>
    <div ng-click="progSubmit()">click me</div>
</form>

angular.module('myApp', [])
.controller('myCtrl', function($scope, $element, $timeout) {
    $scope.submitMe = function() { alert ("hi"); };
    $scope.progSubmit = function() {
        $timeout(function() {
            var inputElem =  $element.find("input");
            angular.element(inputElem[0].form).submit();
          }, 0);
    };
});

Thanks, Yaron

4

1 回答 1

11

所以......如果有人有同样的问题。

解决方案是使用 JQuery 触发提交。理想情况下应该是:

angular.element($scope.inputElem[0].form).trigger('submit');

但是,Angular 中存在一些错误,所以我发现的当前工作是这样的:

angular.element($scope.inputElem[0].form).find('[type=submit]').trigger('click');

干杯。

于 2013-10-20T17:24:52.723 回答