var myapp = angular.module('sampleapp', [ ]); myapp.controller('samplecontoller', function ($scope) { $scope.model = { name:'', lname:'' } $scope.submitForm = function() { alert('Success fully updated : ' + $scope.model.name +' '+$scope.model.lname); } }); 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 fName = attrs.name; scope.$watch(fName + '.$valid', function(val) { if (val) { submitBtn.removeAttr('disabled'); } else { submitBtn.attr('disabled', 'disabled'); } }); } }; } );
<div ng-app="sampleapp"> <div ng-controller="samplecontoller" ng-init="showData()"> <form name="form" disable-btn ng-submit="submitForm()"> <div> <label>First Name:</label> <input type="text" placeholder="First Name" ng-model="model.name" required></input> </div> <div> <label>Last Name:</label> <input type="text"placeholder="Last Name" ng-model="model.lname" required></input> </div> <div> <button class="btn btn-primary" type="submit">Submit</button> </div> </form> </div> </div>