2017-07-19 67 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);