简体   繁体   English

使用Server验证后,从angularjs提交表单

[英]Submit Form from angularjs after validate with Server

I have html form and angular controller. 我有html表单和角度控制器。 In controller I have one function to check user data on the server. 在控制器中,我有一个功能来检查服务器上的用户数据。 and I call this function in button at html by using 我通过使用html中的按钮调用此函数

ng-click

after success , I want submit form from controller. 成功后,我想要从控制器提交表格。

How can I access form.submit from angular controller ? 如何从角度控制器访问form.submit

Thank !!! 谢谢 !!!

I suggest you to use ng-submit directive on your form. 我建议你在表单上使用ng-submit指令。 From there you may return true/false after your validation. 从那里你可以在验证后返回true / false。

Controller 调节器

app.controller('mainController', function($scope) {
  $scope.submit = function(user) {
    var isvalid = true;

    // Add your code 
    // for server validation here

    if (isvalid) {
        return true;
    }
    return false; //failed
  }
});

Html HTML

<form name="userForm" ng-submit="submit(user)">
    <input type="text" ng-model="user.email" />
    <input type="text" ng-model="user.password" />
    <button type="submit">Submit</button>
</form>

Suppose on ng-click you call, 假设你ng-click

validate();

In this function u can do validation and form submission action as follows( my syntax may not be correct....but I'am just trying to convey my idea) 在这个函数中你可以做如下的验证和表单提交操作(我的语法可能不正确......但我只想传达我的想法)

$scope.vaidate = function(input) { 

    //Add your validation code here, it includes api call etc.

    // If validation succeeds, run code for submitting the form. 
    if() // add validation success condition
    {
         //run code for submitting the form.
    }
    else
    {
         // Add code to show error message within the html form.
    }

}

I think you can run submit action within the if condition. 我认为您可以在if条件下运行提交操作。

I have fixed it by using jquery to select form with method submit 我已经使用jquery通过方法提交选择表单来修复它

$("#myForm").submit();

It work fine for me !!! 它适合我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM