AngularJS等待提交表单(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,或者如果你想在你提交的东西结束时做其他事情,你可以使用其他承诺。

$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
$scope.button_readonly = true;
$scope.button_disabled = true;

此时:您的按钮现在可以单击了。 因此,它已准备好被推送(提交(到 PHP

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

}

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

最新更新