在ng-Submit中访问表单元素

时间:2014-01-17 21:35:12

标签: angularjs

我是角度js的新手,我很震惊地访问了ng-submit函数中的表单元素。 我的目的是动态设置action属性并提交表单,而不是使用jquery选择器并设置action属性。

<div ng-app="MyApp">
    <form name="myForm" ng-controller="myController" 
     ng-submit="SubmitFunction(myForm)">
        <input type="submit" value="Submit" />            
    </form>
</div>

JS:
    var myApp = angular.module("MyApp",[]);
    myApp.controller("myController", ["$scope", function ($scope) {
        $scope.SubmitFunction = function (formElement) {
            //Set action attribute ???
           //Submit the form ????
        };           
    }]);

1 个答案:

答案 0 :(得分:17)

感谢您的帮助。终于找到了解决方案。

<div ng-app="MyApp">
    <form name="myForm" ng-controller="myController" 
    ng-submit="SubmitFunction($event)">
        <input type="submit" value="Submit" />            
   </form>
</div>

JS:
var myApp = angular.module("MyApp",[]);
myApp.controller("myController", ["$scope", function ($scope) {
    $scope.SubmitFunction = function (e) {
        var formElement = angular.element(e.target);
        formElement.attr("action", actionLink);
        formElement.submit();
    };           
}]);