【问题标题】:How to submit a form in Angular如何在 Angular 中提交表单
【发布时间】:2014-09-10 16:20:39
【问题描述】:

我有一个要使用 Angular 控制器验证的 html 表单。如果验证失败,我将某些类应用于 html。如果它通过了,我想让表单自己提交。虽然这看起来很简单,但我还没有找到一种简单的方法来做到这一点。我发现的一种方法使用$scope.$broadcast 函数来告诉表单提交,但是,我使用Controller as Ctrl 语法所以我宁愿不使用$scope。无论如何要从控制器提交表单吗?

我的简化 HTML

<form ng-controller="LoginCtrl as login" ng-submit="login.validate()" method="post">
    <input ng-model="login.username" />
    <input ng-model="login.password" />
</form>

JS

var app = angular.module("app", []);

app.controller("LoginCtrl", ["$http", function($http) {
    this.username = "";
    this.password = "";
    this.validate = function() {
        //validate
        if (valid) {
            // somehow form.submit()
        }
    };
}]);

我对 Angular 有点陌生,所以如果这是一个明显的问题,请原谅我;)

编辑: 我需要澄清一下,我希望避免使用 AJAX 提交表单(即$http.post)。基本上我想要的是相当于调用form.submit()的控制器。

用例: 让我准确解释一下我要做什么。

User arrives at login page
User enters credentials
User hits Submit
    Controller asks server (using api path) if the credentials are valid
    if valid then
        Tell the form to submit to regular login path // how?
    else
        Immediately tell the user they submitted invalid credentials

这样,如果用户输入了错误的凭据,他们就会立即获得反馈。 对于实际的表单提交,所有这些我都实现了except

【问题讨论】:

  • 请查看this blog,了解如何在 angularjs 中验证表单
  • 非常有帮助!我肯定会考虑使用指令进行验证。但是,它并没有回答我最初在 Angular 中以编程方式提交表单的问题。
  • 为此,您必须使用 $http,将数据从登录控制器发布到服务器

标签: javascript html forms angularjs


【解决方案1】:

最简单的方法是将所有表单元素数据包装到一个对象中。如果您没有要预填充的数据,则不必创建此对象,ng-model 将为您创建它。

<form ng-controller="LoginCtrl as login" ng-submit="login.validate()" method="post">
    <input ng-model="login.MyFormData.username" />
    <input ng-model="login.MyFormData.password" />
</form>

这将导致控制器范围内的对象如下所示:

$scope.MyFormData={
   username :'foo',
   password:'bar'
}

准备提交时:

$http.post('path/to/server', $scope.myFormData).success(response){
   /* do something with response */
})

【讨论】:

  • 将表单数据放入对象是个好主意。我想过采用这种方法,但我宁愿能够告诉表单提交自己,而不是必须使用 AJAX 提交表单。我应该在我的问题中提到这一点。
【解决方案2】:

我认为您希望将验证作为表单提交流程的一部分。试试这样的:

HTML

<form ng-controller="LoginCtrl as login" ng-submit="login.submit()" method="post">
    <input ng-model="auth.username" />
    <input ng-model="auth.password" />
    <div class="error" ng-hide="valid">Something is invalid...</div>
</form>

JS

var app = angular.module("app", []);

app.controller("LoginCtrl", ["$http", "$scope", function($http, $scope) {
    $scope.valid = true;
    $scope.auth.username = "";
    $scope.auth.password = "";

    var valid = function() {
        // validate

        return valid; // true or false
    };

    this.submit = function() {
        if (valid()) {
            $http.post('/your/auth/url', { auth: auth }).success(function(response) {
                // whatever you're doing to store the auth response.
            });
        } else {
            // use this to conditionally show error messages with ng-show
            $scope.valid = false;
        }
    };
}]);

我不确定我是否理解您关于使用 controller-as 语法的评论。这不应该改变你使用$scope的方式。

【讨论】:

  • 谢谢!但是,这并不能完全回答我的问题。我想知道如何从我的控制器中执行 // submit your form 部分。
  • 您需要使用$scope。我已经相应地更新了我的答案。注意作用域上auth 对象的使用。
【解决方案3】:

我有一个示例,最低代码为here。注意,它是自我验证的,你甚至不需要从控制器提交任何东西!您可以将action和method字段包含为表单属性,如果表单有效,angular会提交表单

HTML

<form name="LoginCtrl as loginForm" method="Post" action="not-a-real-script.php">
  <input type="text" name="name" ng-model="loginData.name" placeholder="username" required="" />
  <input type="password" name="password" ng-model="loginData.password" placeholder="Password" required />
  <input type="submit" ng-disabled="loginForm.$invalid" value="Login" />
</form>

JS

angular.module('app', [])
.controller('LoginCtrl', function($scope) {
  $scope.loginData = {};
});

【讨论】:

  • 一个好主意,但我正在尝试检查用户是否存在于我的后端数据库中,因此它与您的代码正在执行的验证类型不同。
  • 不太清楚你的意思,登录表单被提交,所以后端可以验证用户。我相信您有 2 个选择:像我一样在表单上提交带有 'action=script-url' 属性的表单,或者按照其他人的建议在控制器中使用 $http 服务
  • 查看更新的问题。那么Angular控制器没有办法告诉HTML表单提交自己吗?
  • 您有 2 个选项。 1)做我在上面的 Plunker 示例中所做的事情。当用户输入凭据并点击提交时,表单将自动发送到您的服务器。然后,您的服务器可以在登录后返回重定向消息以将用户发送到页面,如果凭据无效,则返回错误。另一种选择是使用 $http。当用户单击提交时,您向服务器发送 $http 请求,服务器以有效/无效消息响应。然后,在 Angular 控制器内部,如果登录成功,您可以使用 $location 服务重定向到登录页面。
  • 当你在控制器里面时,告诉一个表单提交自己是没有意义的,如果你有 ng-submit="loginHandler()",loginHandler 应该处理所有的登录,表单此时不会对提交做任何其他事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 2016-04-16
  • 1970-01-01
  • 2015-11-20
  • 2020-02-18
  • 1970-01-01
  • 2016-04-09
相关资源
最近更新 更多