Tôi đã trả lời câu hỏi này trong this StackOverflow article, nhưng đây là một bản tóm tắt về những gì tôi đã làm.
Nếu bạn phong cách mã của bạn một cách chính xác, và chắc chắn rằng tất cả các cuộc gọi đến một đường chuyền dịch vụ web thông qua một đặc biệt factory
chức năng, sau đó bạn có thể làm cho điều đó factory
chức năng xử lý hiển thị và ẩn của bạn "Please Wait" popup.
Đây là factory
chức năng mà tôi sử dụng để gọi tất cả các dịch vụ web GET của tôi:
myApp.factory('httpGetFactory', function ($http, $q) {
return function (scope, URL) {
// This Factory method calls a GET web service, and displays a modal error message if something goes wrong.
scope.$broadcast('app-start-loading'); // Show the "Please wait" popup
return $http({
url: URL,
method: "GET",
headers: { 'Content-Type': undefined }
}).then(function (response) {
scope.$broadcast('app-finish-loading'); // Hide the "Please wait" popup
if (typeof response.data === 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function (errorResponse) {
scope.$broadcast('app-finish-loading'); // Hide the "Please wait" popup
// The WCF Web Service returned an error.
// Let's display the HTTP Status Code, and any statusText which it returned.
var HTTPErrorNumber = (errorResponse.status == 500) ? "" : "HTTP status code: " + errorResponse.status + "\r\n";
var HTTPErrorStatusText = errorResponse.statusText;
var message = HTTPErrorNumber + HTTPErrorStatusText;
BootstrapDialog.show({
title: 'Error',
message: message,
buttons: [{
label: 'OK',
action: function (dialog) {
dialog.close();
},
draggable: true
}]
});
return $q.reject(errorResponse.data);
});
};
});
này sẽ được gọi như thế này:
myApp.webServicesURL = "http://localhost:15021/Service1.svc";
var dsLoadAllEmployees = function (scope)
{
// Load all survey records, from our web server
$scope.LoadingMessage = "Loading Employees data...";
var URL = myApp.webServicesURL + "/loadAllEmployees";
return httpGetFactory(scope, URL);
}
Đây là "Xin chờ" kiểm soát mà tôi sử dụng trên mỗi trang ..
<please-wait message="{{LoadingMessage}}" ></please-wait>
... và mã của nó trông như thế này ...
myApp.directive('pleaseWait',
function ($parse) {
return {
restrict: 'E',
replace: true,
scope: {
message: '@message'
},
link: function (scope, element, attrs) {
scope.$on('app-start-loading', function() {
element.fadeIn();
});
scope.$on('app-finish-loading', function(){
element.animate({
top: "+=15px",
opacity: "0"
}, 500);
});
},
template: '<div class="cssPleaseWait"><span>{{ message }}</span></div>'
}
});
Sử dụng cấu trúc này, bất kỳ bộ điều khiển góc của tôi có thể tải dữ liệu từ một dịch vụ web chỉ trong một vài dòng, và rời khỏi nhà máy để chăm sóc hiển thị/ẩn "Xin chờ" tin nhắn và để hiển thị bất kỳ lỗi xảy ra:
$scope.LoadAllSurveys = function() {
DataService.dsLoadAllSurveys($scope).then(function (response) {
// Success
$scope.listOfSurveys = response.GetAllSurveysResult;
});
}
Tốt, này?
Trong trường hợp bạn muốn hiển thị chỉ báo tải cho cuộc gọi dịch vụ web, đó cũng là cách để chặn cuộc gọi http. Có một số bài đăng trên blog về cách triển khai trình chặn như vậy. Một điều tốt là: http://codingsmackdown.tv/blog/2013/01/02/using-response-interceptors-to-show-and-hide-a-loading-widget/ – Stephan