I created a "message center" in Angularjs that injects messages on the screen. The message always appears when an error occurs in the REST or success operations. This is done by an interceptor that checks whether the response has a message header, if it is, it injects the message and type into the provider, and the policy prints to the screen.
The problem is that the message appears in the Modal, which is right, and also appears on the page that called the modal the first time I open the page the other times does not appear.
Second,afterclosingthemodalandthemessagethatappearedonthelistscreen:
Interceptor(whichissetupin$httpProvider):
angular.module('app').factory('notificationInterceptor',function($q,AlertService){return{responseError:function(response){addMessage(response);return$q.reject(response);},response:function(response){addMessage(response);returnresponse;}};functionaddMessage(response){varalert=response.headers('X-applicationMessage');if(angular.isString(alert)){varmensagens=response.data.mensagens;vartimeout=10000;if(mensagens!=undefined){mensagens.forEach(function(element,index,array){AlertService.add(element.messageType,element.body);});}}}
Provider
'usestrict'angular.module('app').provider('AlertService',function(){this.$get=[function(){varalerts=[];varexports={add:add,clear:clear,get:get}functionclear(){alerts=[];}functionget(){returnalerts;}functionadd(type,msg){varalert={type:type,msg:msg,};alerts.push(alert);returnalert;}returnexports;}];});
Policy
'usestrict';angular.module('app').directive('mcAlert',function(AlertService){return{restrict:'E',template:'<uib-alertng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>',
controller : [ '$scope', function($scope) {
$scope.alerts = AlertService.get();
$scope.$on('$destroy', function() {
AlertService.clear();
});
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
}
}]
}
});