5

Uygulamamda bir hata işleme parçası yazmak istiyorum Aşağıdaki kodu kullanıyorum ama hata 500 işini doğru yaptığınızda ancak küçük veya belki büyük bir sorun var ve bu sayfa ilk olarak yüklenen ve birkaç saniye hata sayfası yüklendikten sonra, bu birkaç saniyeyi nasıl kaldırabilirim ve hata veren ana sayfa yüklenmeden hata sayfasına giderim? denetleyicisini çalıştırdıktan sonra html şablonunu yüklemek için herhangi bir yolu var mı?angularjs anjax işleminde hata işleme

var interceptor = ['$rootScope', '$q', function (scope, $q) { 

     function success(response) { 
      return response; 
     } 

     function error(response) { 
      var status = response.status; 
      if (status == 500) { 
       window.location= "http://www.domain.lan/#!/error"; 


       return; 
      } 
      if (status == 403) { 

       // window.location = "dashboard"; 
       return; 
      } 
      // otherwise 
      return $q.reject(responseInterceptors); 

     } 

     return function (promise) { 
      return promise.then(success, error); 
     } 

    }]; 
    $httpProvider.responseInterceptors.push(interceptor); 
+0

$ injector.get ('$ state') öğesini kullanır.Git ('error'); ama bu sorun üzerinde herhangi bir etkisi yok –

+0

Neden böyle bir hata $ routeProvider/$ stateProvider kendisi işlemez. –

+0

Bunu nasıl yapabilirim? im new in angularjs –

cevap

3

Köşeli yönlendiriciyi kullandığınızı varsayalım.

$ stateProvider yapılandırmanızda bir ui yönlendiriciden 'hata' durumunu anlamak için bir durum eklemeniz gereken ilk şey.

Rota yapılandırma Kod

//added state to understand error 
$stateProvider.state("error": { 
    url: "/error", 
    templateUrl: '/views/error.html', 
    controller: 'errorCtrl' //optional if you want any specific functionality 
});   

And You window.location yaptı ve yenilemek için sayfayı neden url, window.location ayarlayın. Bunun yerine Window.location kullanım window.location.hash

Kesişme fonksiyon değişikliği

var interceptor = ['$rootScope', '$q', '$location', function (scope, $q, $location) { 
function success(response) { 
    return response; 
} 

function error(response) { 
    var status = response.status; 
    if (status == 500) { 
     //window.location= "http://www.domain.lan/#!/error"; 
     //window.location.hash= "/error"; //it will rewrite the url without refreshing page 
     $location.path('error'); 
     return; 
    } 
    if (status == 403) { 

     // window.location = "dashboard"; 
     return; 
    } 
    // otherwise 
    return $q.reject(responseInterceptors); 

} 

return function (promise) { 
    return promise.then(success, error); 
} 

}]; 
$httpProvider.responseInterceptors.push(interceptor); 

Diğer yolu kullanmanın aynı $ state.go ('hatası') deneyebilirsiniz; $ state dependancy eklemeyi unutma.

Bu size yardımcı olacaktır umuyoruz. Hala herhangi bir karışıklık varsa bildirin.

+0

yapmak için kötü bir çözüm düşünün config $ showProvider.hashPrefix ('!') içinde hashprifix kullanıyorum; ve window.location.hash hata yapar, bu sorunu nasıl çözebilirim? –

+0

@mhadadi, güncellenmiş kodu deneyin –

+0

$ state.go ve $ konumlarını kullanıyorum ama her ikisi de daha önce yazımda daha önce dediğim problemi, ilk şablon yükünü ve ajax isteğini gönderdiler ama ajax 404'ü döndürdüler ve bir boşluk boş kaldı ikinci sayfa hata durumuna değiştirmek istiyorum bu birkaç saniye kaldırmak istiyorum ve kullanıcı gösterge tablosu sayfasına gittiğinde (ve burada bazı hata var) herhangi bir kesinti olmadan hata sayfasına gitmek –

İlgili konular