Javascript AngularJS-$http和通知服务
我正在尝试在angularJS上构建一个简单的通知服务:Javascript AngularJS-$http和通知服务,javascript,angularjs,Javascript,Angularjs,我正在尝试在angularJS上构建一个简单的通知服务: angular.module("my.services", []) .service("NotificationsService", function() { this.show = function(message, options) { // display a notification }; this.error = function(messag
angular.module("my.services", [])
.service("NotificationsService", function() {
this.show = function(message, options) {
// display a notification
};
this.error = function(message) {
this.show({...});
}
...
});
当服务器返回嵌入在api中的“通知”数组时,将触发:
{
notifications: [{type: "error", message: "Error message"}, ...],
data: [item1, item2, ...]
}
现在我想将我的服务插入到$http,但我找不到一种方法
有什么想法吗?使用a,并将您的通知服务
注入其中:
angular.module("my.services", [], function ($httpProvider) {
$httpProvider.responseInterceptors.push(function (NotificationsService) {
function notify ( response ) {
angular.forEach(response.notifications, function ( obj ) {
NotificationsService.show( obj );
});
return response;
}
return function ( promise ) {
return promise.then(notify, notify);
}
});
});
伟大的谢谢你的帮助!:)