2017-07-19 63 views
1

我有一个表格,在这里我提交这个表格时,它加载。AngularJS等待提交表格(ng-submit)

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

<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> 

我的控制器代码

我的HTML页面的代码下面给出

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。

+0

通过给定指定的间隔等待并调用submitform方法,使用$ timeout方法。 –

回答

0

你可以尝试对角

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

使用$超时方法超时服务自动调用后的特定时间间隔提交

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

我必须在控制器内部使用它吗? – user3441151

+0

是只在你的控制器里面 –

+0

但是提交表单后提到控制器,它是否正确? – user3441151

0

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

+0

你能解释一下我必须写这段代码吗? – user3441151

+0

是的,你是对的。把它放在你的控制器里面。并且不要忘记在你的控制器中注入'$ timeout'。 – voskhod

0

也许你可以使用$超时或者如果你想要做些别的事情,当这个东西结束您提交,您可以使用其他Promises。

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

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

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

喜欢这张 .controller( '的AppController',[ '$ rootScope', '$范围', '$状态',“$ window','globalService','dataServices','$ timeout',函数($ 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);