请问angular1的脚本提交表单是怎么写的?

比如点击某个事件触发表单提交;
$scope.submit_click = function(){

            //....这里怎么写提交form表单的方法?
        }

类似jq的
$(".div").submit()

如果用angular的做法是怎么写?

阅读 2.9k
3 个回答

首先angular不建议这么做哟 !用$http提交ajax请求代替,因为submit了页面就会刷新。

//可以这样用,实际上还是调用ajax请求。

<form ng-submit="myFunc()">
  <input type="text">
  <input type="submit">
</form>

<p>{{myTxt}}</p>


<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.myTxt = "你还没有点击提交!";
  $scope.myFunc = function () {
      $scope.myTxt = "你点击了提交!";
  }
});
</script>

<form method="post">

 <input type="text" ng-model="validate.mobile">
 <input type="text" ng-model="validate.name">

</form>
<div ng-click="submitform(validate)">确定提交</div>
<script>

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.submitform= function (validate) {
        //some code here
    }    

}
});
</script>
//希望对你有用

表单

<div ng-app="myApp" ng-controller="myCtrl">
    <div id="form-name" class="formParam">
        <span>Username:</span><input type="text" ng-model="formParam.username"/>
    </div>
    <div id="form-pwd" class="formParam"> 
        <span>Password:</span><input type="password" ng-model="formParam.password"/>
    </div>
    <button type="submit" ng-click="handler()">Login</button>
</div>

对应angular代码

var myApp = angular.module('myApp', []);
    myApp.controller('myCtrl', function($scope, $http){
        $scope.formParam = {};
        $scope.handler = function(){
            $http({
                method: 'POST',
                url: '/handler',
                data: formParam
            }).then(success(response){
                // 请求成功相关处理
            }, fail(response){
                //  请求失败相关处理
            })
        };
    });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题