admin管理员组文章数量:1391981
I'm trying to build a simple notification service on angularJS :
angular.module("my.services", [])
.service("NotificationsService", function() {
this.show = function(message, options) {
// display a notification
};
this.error = function(message) {
this.show({...});
}
...
});
That would be fired when the server returns a "notifications" array embedded in the api :
{
notifications: [{type: "error", message: "Error message"}, ...],
data: [item1, item2, ...]
}
And now I would like to plug my service to $http, but I can't find a way to do so !...
Any ideas ?
I'm trying to build a simple notification service on angularJS :
angular.module("my.services", [])
.service("NotificationsService", function() {
this.show = function(message, options) {
// display a notification
};
this.error = function(message) {
this.show({...});
}
...
});
That would be fired when the server returns a "notifications" array embedded in the api :
{
notifications: [{type: "error", message: "Error message"}, ...],
data: [item1, item2, ...]
}
And now I would like to plug my service to $http, but I can't find a way to do so !...
Any ideas ?
Share Improve this question asked Jan 13, 2013 at 23:17 TiuShTiuSh 1452 silver badges9 bronze badges1 Answer
Reset to default 8Use a Response interceptor, and inject your NotificationsService
into it:
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);
}
});
});
本文标签: javascriptAngularJShttp and notification serviceStack Overflow
版权声明:本文标题:javascript - AngularJS - $http and notification service - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744689829a2619925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论