Disable submit button when form invalid with AngularJS

后端 未结 6 880
不思量自难忘°
不思量自难忘° 2020-11-28 19:31

I have my form like this:

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 20:11

    We can create a simple directive and disable the button until all the mandatory fields are filled.

    angular.module('sampleapp').directive('disableBtn',
    function() {
     return {
      restrict : 'A',
      link : function(scope, element, attrs) {
       var $el = $(element);
       var submitBtn = $el.find('button[type="submit"]');
       var _name = attrs.name;
       scope.$watch(_name + '.$valid', function(val) {
        if (val) {
         submitBtn.removeAttr('disabled');
        } else {
         submitBtn.attr('disabled', 'disabled');
        }
       });
      }
     };
    }
    );
    

    For More Info click here

提交回复
热议问题