繁体   English   中英

AngularJS等待提交表单(ng-submit)

[英]AngularJS wait submit the Form (ng-submit)

我有一个表格,在此表格中,我会在加载时提交此表格。

我希望在加载此页面时等待几秒钟,然后提交此表单。 我不想使用任何按钮提交此表单,它应该是“自动提交”表单。 可能吗?

我的HTML页面代码如下

<form name="main" ng-submit="submitForm(main_1)">
    <table align="center">
        <tr>
            <td>First Name :</td>
            <td>
                <input type="text" id="txtFirstname" name="txtFirstname" ng-model="verifyData.firstName" /></td>
            <td>Middle Initial :</td>
            <td>
                <input type="text" id="txtMiddlename" name="txtMiddlename" ng-model="verifyData.middleInitial" /></td>
        </tr>
    </table>
    <img src="images/loader.gif" id="loading-image" ng-if="showLoader">
    <div id="load1" ng-if="showLoader"></div>
</form>

我的控制器代码如下

angular.module(appConfig.appName)
    .controller('appController', ['$rootScope', '$scope', '$state', '$window', 'globalService', 'dataServices', function ($rootScope, $scope, $state, $window, globalService, dataServices) {
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            window.history.forward();
        });

        $scope.showLoader = false;
        var firstName = document.getElementById('txtFirstname');
        if (firstName != null) {
            var lastName = document.getElementById('txtLastname');
        }

        $scope.submitForm = function () {
            $scope.showLoader = true;
            dataServices.verifyData($scope.verifyData).then(function (response) {
                $state.go(response.redirectURL);
            }, function (res) {
                $rootScope.serviceErrorMsg = res.error;
                $state.go(res.redirectURL);
            });
        }

        if ($scope.verifyData) {
            $scope.submitForm();
        }
    }])
]);

我正在使用AngularJS。

您可以尝试Angular的超时服务

$timeout(function () {
        $scope.submitForm();
    }, 2000);

使用$ timeout方法调用特定时间间隔后自动提交

$timeout( function(){
        // call submitform method
    }, 5000 );

在控制器初始化2秒后,使用$ timeout提交表单: $timeout($scope.submitForm, 2000)

也许您可以使用$ timeout,或者如果您要在提交结束时再做其他事情,则可以使用Promises。

$timeout( function(){ // code }, 3000 );

要么

Some_Function().then(function(){ //YourCode });

Write the code in $timeout (Note :- Just Insert $timeout as a dependency otherwise it will give error)

像这样.controller('appController',['$ rootScope','$ scope','$ state','$ window','globalService','dataServices,'$ timeout',function($ rootScope,$ scope ,$ state,$ window,globalService,dataServices,$ timeout)

For example :- 
$scope.submitForm = function () {
            $scope.showLoader = true;
            dataServices.verifyData($scope.verifyData).then(function (response) {
                $state.go(response.redirectURL);
            }, function (res) {
                $rootScope.serviceErrorMsg = res.error;
                $state.go(res.redirectURL);
            });
        }
$timeout(function(){
 $scope.submitForm();
}, 5000);

一个简单易用的方法就是这样做

在您的视图(HTML)中-禁用提交按钮
<button ng-readonly="button_readonly" ng-disabled="button_disabled" name="Submit">SUBMIT</button>


在控制器中,这些值最初设置为false
$scope.button_readonly = false;
$scope.button_disabled = false;

因为角度数据绑定。 然后您可以执行逻辑。
如果逻辑成功返回(无论是ajax还是输入验证等),则可以将上述值设置为true


成功时:
设为true
$scope.button_readonly = true;
$scope.button_disabled = true;

此时:您的按钮现在可以单击。 因此,准备将其推送(提交)到php

在PHP中
if(isset($_POST['Submit'])){
//do your thing
}

回答您的问题。 由于您不想使用按钮,因此将hidden属性添加到按钮。 然后定期检查值是否已更改。 当他们这样做时,添加提交逻辑。 即。 如果button_readonly = true; 提交

暂无
暂无

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

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