我可以触发控制器提交的表单吗?

时间:2013-04-14 19:11:38

标签: forms angularjs submit

我想在控制器内做一个传统的表单提交。场景是我想在我的网络服务器上点击一条路线并重定向到它的响应,我可以用HTML中的常规表单来做,但我也希望在按下提交按钮时对其字段进行一些验证,并且如果验证失败,我不想做路线。

我知道ng-valid,但我只想在按钮被击中时进行验证。

有没有办法从控制器中有条件地进行表单提交?

6 个答案:

答案 0 :(得分:34)

您可以将提交方法添加到FormController。我这样做了:

<form ng-form-commit action="/" name='payForm' method="post" target="_top">
    <input type="hidden" name="currency_code" value="USD">
    <button type='button' ng-click='save(payForm)'>buy</button>
</form>

.directive("ngFormCommit", [function(){
    return {
        require:"form",
        link: function($scope, $el, $attr, $form) {
            $form.commit = function() {
                $el[0].submit();
            };
        }
    };
}])

.controller("AwesomeCtrl", ["$scope", function($scope){
   $scope.save = function($form) {
     if ($form.$valid) {
         $form.commit();
     }
   };
}])

答案 1 :(得分:8)

您是否尝试在表单上使用ng-submit指令?您可以在验证后返回true / false。

控制器

app.controller('MainCtrl', ['$location', function($scope, $location) {
  $scope.submit = function(user) {
    var isvalid = true;
    // validation 
    if (isvalid) {
        $http.get('api/check_something', {}).then(function(result) {
            $location.path(result.data);
        });
        return true;
    }
    return false; //failed
  }
});

Html(你必须没有动作属性)

<form name="formuser" ng-submit="submit(user)">
    <input type="text" ng-model="user.firstname" />
    <input type="text" ng-model="user.lastname" />
    <button type="submit">Submit</button>
</form>

答案 2 :(得分:3)

这不是Angular&#39;这样做但你可以使用vanilla javascript提交表单。例如,您可以为表单提供一个id并执行:

<a class="item" href="http://localhost/"> <i class="fa fa-home"></i> <span class="label">Home</span> </a>

或者如果您有提交按钮,则可以单击它:

document.getElementById('myForm').submit()

我发现第一个没有保留数据绑定(我在一个没有Angular替代的JQuery小部件的项目中使用它),但第二个保留了绑定。我认为这与编写JQuery小部件的方式有关。

您可以在此处查看有关使用vanilla JS触发表单的更多信息:

How to submit a form using javascript?

答案 3 :(得分:0)

在窗体有效之前,如何直接禁用提交按钮:

<button type="submit" ng-disabled="form.$invalid">Submit</button>

看看我遇到的类似问题: AngularJS Form Validation with Directives - "myform.$valid" not quite right for me

答案 4 :(得分:0)

从@ ReklatsMasters的答案扩展,如果您想在提交表单之前更改值,您可以这样做......

<form ng-form-commit action="/" name='payForm' method="post" target="_top">
    <input type="hidden" id="currency_code" name="currency_code" value="USD">
    <button type='button' ng-click='save('GBP', payForm)'>buy</button>
</form>

.directive("ngFormCommit", [function(){
    return {
        require:"form",
        link: function($scope, $el, $attr, $form) {
            $form.commit = function($newCurrency) {
                $el[0].querySelector('#currency_code').value = $newCurrency;
                $el[0].submit();
            };
        }
    };
}])

.controller("AwesomeCtrl", ["$scope", function($scope){
   $scope.save = function($newCurrency, $form) {
     if ($form.$valid) {
         $form.commit($newCurrency);
     }
   };
}])

答案 5 :(得分:0)

$scope.payForm.$setSubmitted();

将表单设置为$ submitted状态。这还将在该表单的所有子表单和父表单上设置$ submitted

https://docs.angularjs.org/api/ng/type/form.FormController#$setSubmitted

相关问题