如何检查禁用按钮是true / false?在angularjs

时间:2016-09-06 12:59:23

标签: javascript angularjs angularjs-directive angularjs-scope

大家好,有没有办法如何检查禁用按钮?

这是我的按钮禁用代码(index.html):

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

在控制器中:

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

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

2 个答案:

答案 0 :(得分:0)

您可以使用angular.element(..).prop()

禁用查看按钮
var target = angular.element(document.getElementById("#buttonIdHere"));
if (target.prop('disabled')) {
    // Button is disabled
}

选项2 关注yourForm.$validyourForm.$invalid&控制器中的yourForm.$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;
});

在执行提交逻辑之前,只需检查isFromValid是否为真。

答案 1 :(得分:0)

如果您需要检查submit的逻辑,那么您可以将表单对象传递给处理程序submit

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>
&#13;
&#13;
&#13;