0
I created a "message center" in Angularjs that injects messages on the screen. The message always appears when an error occurs in REST operations or success. This is done by an Interceptor that checks if the response has a message header, if it is injected into the message and type, and the directive prints on the screen.
The problem is that the message appears in 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 time, after closing the modal and the message that appeared on the listing screen:
Interceptor (which is configured in $httpProvider):
angular.module('app').factory('notificationInterceptor', function ($q, AlertService) {
return {
responseError: function(response) {
addMessage(response);
return $q.reject(response);
},
response: function(response) {
addMessage(response);
return response;
}
};
function addMessage(response){
var alert = response.headers('X-applicationMessage');
if (angular.isString(alert)) {
var mensagens = response.data.mensagens ;
var timeout = 10000;
if(mensagens!= undefined){
mensagens.forEach(function(element, index, array) {
AlertService.add(element.messageType, element.body);
});
}
}
}
Provider
'use strict'
angular.module('app').provider('AlertService',
function() {
this.$get = [ function() {
var alerts = [];
var exports = {
add : add,
clear : clear,
get : get
}
function clear() {
alerts = [];
}
function get() {
return alerts;
}
function add(type, msg) {
var alert = {
type : type,
msg : msg,
};
alerts.push(alert);
return alert;
}
return exports;
} ];
});
Directive
'use strict';
angular
.module('app')
.directive(
'mcAlert',
function(AlertService) {
return {
restrict : 'E',
template: '<uib-alert ng-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);
}
}]
}
});
How is the html of the modal and the page that calls the modal?
– Vinicius Zaramella
Do you want to see it all? or just the directive part?
– adelmo00
Only the party having the directive
– Vinicius Zaramella
I called it this way: <mc-Alert />. If you want the code that is generated I put.
– adelmo00
I wanted to know if you actually use the directive on both the modal and the page, but I think it’s kind of obvious that you do. What I think is happening here is that you have a Service that feeds the Alerts in more than one place. Since Service is a single instance it must be passing the list of Alerts to the two places you use the directive
– Vinicius Zaramella
@Viniciuszaramella is exactly what you said. However the strange is the problem only occurs the first time, in the other the alert only appears in modal.
– adelmo00
I think this has to do with the $Stroy you created. By calling it the two pages lose reference to the list that is within Alertservice. When you open the modal again it picks up the reference again, but the main page does not.
– Vinicius Zaramella
I can take the $Scope from the current page and put the alerts inside it through the directive?
– adelmo00
@Viniciuszaramella I can get $Scope from the current controller and put the alerts inside it through the directive?
– adelmo00
I believe you will need to create an attribute in your directive that receives a function from the controller Scope that is making the request to update the list of Alerts. I’ll try to make an answer.
– Vinicius Zaramella