2015-11-03 90 views
0

我想保持我的提交按鈕disabled直到形式是HTML有效,我已經成功地做到像這樣:AngularJS:提交禁止,直到HTML 5的有效和禁止提交後防止多提交

<button type="submit" class="btn" ng-disabled="mainPaymentForm" ng-click="disableButton()"><%button%></button> 

在我控制器我有這樣的:

// Disable form on submit 
$scope.disableButton = function(){ 
     $scope.button = "Loading..."; 
     $scope.submit.disable = true; // This doesn't work 
}; 

總的來說,我只是想保持按鈕禁用,直到有效/和防止多重提交點擊一次有效。

感謝

回答

1

我想通了:

HTML代碼

ng-disabled="mainPaymentForm.$invalid || clickDisable"

JS控制器代碼

// this will unable button if form becomes valid 
$scope.$watch('mainPaymentForm.$invalid', function(){ 
     $scope.clickDisable = false; 
}); 

// this will disable it on click 
$scope.disableButton = function(){ 
     $scope.button = "Loading..."; 
     $scope.clickDisable = true; 
};