我为应用程序全局创建通知服务,同时发送数据库调用,捕获拦截器中的错误代码并显示警报。不允许在中继器中复制?使用'track by'表达式来指定唯一的密钥。
如何避免角度通知服务中的重复错误消息。 拦截代码
var interceptor = function ($q, alerts, $rootScope, $timeout, $location, alertsManager) {
return {
request: function (config) {
console.log(config);
return config;
},
response: function (response) {
var deferred = $q.defer();
//$rootScope.$broadcast('loginRequired');
//$scope.alerts.push({ msg: "Request done !" });
return response || $q.when(response);
},
responseError: function (rejection) {
if (rejection.status == 500)
{
var deferred = $q.defer();
$rootScope.$broadcast('loginRequired');
return $q.reject(rejection);
}
console.log(rejection.status);
return $q.reject(rejection);
}
}
};
$httpProvider.interceptors.push(interceptor);
控制器调用服务器呼叫响应里面。
LoginService.AfterLogin(UserName, Password)).then(function (response) {
},function (status) {
if (status === 500) {
alert("200");
$rootScope.$on("loginRequired", function (e) {
alertsManager.addAlert('Technical Error Occurred.Please contact the System Administrator for the further support!!', 'alert-success');
});
}
});
工厂提醒服务
App.factory('alertsManager', function() {
return {
alerts: {},
addAlert: function (message, type) {
this.alerts[type] = this.alerts[type] || [];
this.alerts[type].push(message);
},
clearAlerts: function() {
for (var x in this.alerts) {
delete this.alerts[x];
}
}
};
});
方法一:
<div ng-controller="AlertsController">
<div ng-repeat="(key,val) in alerts" class=" alert {{key}}" id="alert-notify">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div ng-repeat="msg in val">{{msg}}</div>
</div>
</div>
<div ng-controller="AlertsController">
<div ng-repeat="(key,val) in alerts track by $index" class=" alert {{key}}" id="alert-notify">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div ng-repeat="msg in val track by $index">{{msg}}</div>
</div>
</div>
你为什么标记它作为angular2? – yurzui
最后一个例子不正确吗?你能显示实际的数据,所以我们知道它应该是什么样子? – tasseKATT
我没有收到你说的话,2次错误响应触发并显示重复错误。 –