简体   繁体   English

如何检查禁用按钮为真/假? 在angularjs中

[英]How to check the disabled button is true/false ? in angularjs

Hi guys is there any ways on how to check the disable button ?? 大家好,有什么方法可以检查禁用按钮?

Here is my button disabled code inside (index.html): 这是我里面禁用按钮的代码(index.html):

ng-disabled="epUpdateAllForm.$invalid || epUpdateAllForm.$pending"

In controller : 在控制器中:

    if((($scope.epUpdateAllForm.$invalid)  || ($scope.epUpdateAllForm.$pending)) == true) {

        console.log("Some mandatory fields is not completed");
    }  

You can check button is disabled using angular.element(..).prop() 您可以使用angular.element(..).prop()检查按钮是否已禁用

var target = angular.element(document.getElementById("#buttonIdHere"));
if (target.prop('disabled')) {
    // Button is disabled
}

Option 2 Watch for yourForm.$valid , yourForm.$invalid & yourForm.$pending in your controller 选项2yourForm.$valid监视yourForm.$validyourForm.$invalidyourForm.$pending

var isFormValid = false;
$scope.$watch('yourForm.$valid', function(newVal) {
    isFormValid = true;
});

$scope.$watch('yourForm.$invalid', function(newVal) {
    isFormValid = false;
});

$scope.$watch('yourForm.$pending', function(newVal) {
    isFormValid = false;
});

And simply check if isFromValid is true before executing your submission logic. 只需在执行提交逻辑之前检查isFromValid是否为true。

If you need to check the logic of submit , then you can pass a form object to the handler submit . 如果您需要检查的逻辑submit ,那么你可以通过表单对象来处理程序submit

Example on jsfiddle . jsfiddle上的示例

 angular.module('ExampleApp', []) .controller('ExampleController', function($scope) { $scope.submit = function(form) { if (form.$invalid) console.warn('Form is invalid'); else console.log('Form is valid'); }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="ExampleApp"> <div ng-controller="ExampleController as vm"> <form name="testForm"> <label> This field is required <input type="text" name="testInput" ng-model="myInput" required> </label> <button ng-click="submit(testForm)"> Submit </button> </form> </div> </div> 

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

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