If I update or submit a form, the button should be disabled until the http response has finished. In the same way, when a page is loading, the button should be disabled until the entire data is loaded from the server.
In my code, the button is not disabled. How would I go about implementing it?
index.jsp
<body ng-app="myApp" ng-controller="myCtrl">
<button ng-click="save()" loading="Loading..." notloading="save" disableonrequest></button>
</body>
script.js
// the main (app) module
var myApp = angular.module("myApp", []);
// add a controller
myApp.controller("myCtrl", function($scope, $http, $timeout) {
$scope.save = function() {
// JUST FOR TESTING
$http.pendingRequests.length = 1;
$timeout(function() {
$http.pendingRequests.length = 0;
}, 1000);
};
});
myApp.directive("disableonrequest", function($http) {
return function(scope, element, attrs) {
console.log(scope, element, attrs)
scope.$watch(function() {
return $http.pendingRequests.length > 0;
}, function(request) {
console.log(request);
if (!request) {
element.html("<span >" + attrs.notloading + "</span>");
} else {
element.html("<span >" + attrs.loading + "</span><i class='fa fa-spinner fa-spin'></i>");
}
});
}
});
How can I disable the button until it has loaded or the request has started?