0

我为应用程序全局创建通知服务,同时发送数据库调用,捕获拦截器中的错误代码并显示警报。不允许在中继器中复制?使用'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">&times;</span></button> 
       <div ng-repeat="msg in val">{{msg}}</div> 
      </div> 
</div> 

enter image description here 方法一: 方法2: 当我使用的轨道由$指数,得到复制。

<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">&times;</span></button> 
       <div ng-repeat="msg in val track by $index">{{msg}}</div> 
      </div> 
</div> 

enter image description here

+1

你为什么标记它作为angular2? – yurzui

+0

最后一个例子不正确吗?你能显示实际的数据,所以我们知道它应该是什么样子? – tasseKATT

+0

我没有收到你说的话,2次错误响应触发并显示重复错误。 –

回答

0

尝试使用味精(或msg.someKey当它是一个对象),而不是$指数

<div ng-repeat="msg in val track by msg">{{msg}}</div> 

但味精需要是不相同的。试试这个

<div ng-repeat="(key,val) in alerts track by key" class=" alert {{key}}" id="alert-notify"> 
       <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <div ng-repeat="msg in val track by $index">{{msg}}</div> 
      </div> 

$指数在符合第二中继良好

+0

'$ index'也应该与对象一起工作。 – tasseKATT

+0

如果我给出像上面截图所示的重复消息,因为错误函数触发了3次。 –

相关问题