2016-02-24 64 views
1

我有這樣的模板:追趕NG-重複重複錯誤

<li ng-repeat="item in results.items track by item.id"><ul result-list-line></ul></li> 

有時候我Duplicates in a repeater are not allowed。基本上是一個後端問題,但我需要在FE中處理它。

現在,我知道它已經被討論過了:我可以循環數據並捕獲重複自己,或者我可以通過使用track by $index來繞過它。 我不想做 - 我想要捕捉Angular錯誤並處理它(基本上顯示錯誤消息)。我怎麼做?

這裏是我的版本低於斯捷潘Kasyanenko的回答是:

.factory('$exceptionHandler', [ '$injector', function ($injector) { 
    return function (exception, cause) { 
     // Preventing circular reference when using $rootScope 
     var $rootScope = $injector.get('$rootScope'); 
     var msg = exception.message; 


     // Still have the usual console error - extend instead of replace 
     var $log = $injector.get('$log'); 
     $log.error.apply($log, arguments); 

     //catching ngRepeat duplicates 
     if (msg.search("ngRepeat:dupes") > -1) { 
      msg = "Duplicate entries were sent from the server" 
     } 

     // Not always you get a cause string, but if so you might want to add it to the message 
     if (cause) { 
      msg += ". Cause: "+cause; 
     } 

     // No matter what I did, I couldn't inject any factory that uses $rootScope, so I used custom event instead 
     $rootScope.$emit("angularError", msg); 
    }; 
}]) 

回答

1

您可以使用$exceptionHandler

關於jsfiddle的現場示例。

angular.module('ExampleApp', []) 
 
    .controller('ExampleController', function($scope) { 
 
    $scope.countries = [{ 
 
     countryName: 'United States', 
 
     countryCode: 1 
 
    }, { 
 
     countryName: 'Canada', 
 
     countryCode: 2 
 
    }, { 
 
     countryName: 'Bahamas', 
 
     countryCode: 3 
 
    }, { 
 
     countryName: 'Chile', 
 
     countryCode: 4 
 
    }, { 
 
     countryName: 'Chile', 
 
     countryCode: 4 
 
    }]; 
 

 
    }) 
 
    .factory('$exceptionHandler', function() { 
 
    return function(exception, cause) { 
 
     alert(exception) 
 
    } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="ExampleApp"> 
 
    <div ng-controller="ExampleController"> 
 

 
    <label>Country:</label> 
 
    <div ng-repeat="country in countries track by country.countryCode"> 
 
     {{country.countryName}} 
 
    </div> 
 
    </div> 
 
</div>

修訂

$exceptionHandler手柄也自定義錯誤。

angular.module('ExampleApp', []) 
 
    .controller('ExampleController', function($scope) { 
 
    $scope.countries = [{ 
 
     countryName: 'United States', 
 
     countryCode: 1 
 
    }, { 
 
     countryName: 'Canada', 
 
     countryCode: 2 
 
    }, { 
 
     countryName: 'Bahamas', 
 
     countryCode: 3 
 
    }, { 
 
     countryName: 'Chile', 
 
     countryCode: 4 
 
    }, ]; 
 
    $scope.raiseError = function() { 
 
     throw "my raiseError"; 
 
    } 
 
    $scope.addBadElement = function() { 
 
     $scope.countries.push({ 
 
     countryName: 'Chile', 
 
     countryCode: 4 
 
     }); 
 
    } 
 
    }) 
 
    .factory('$exceptionHandler', function() { 
 
    return function(exception, cause) { 
 
     alert(exception); 
 
    } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="ExampleApp"> 
 
    <div ng-controller="ExampleController"> 
 

 
    <label>Country:</label> 
 
    <button ng-click="raiseError()"> 
 
     raise Error 
 
    </button> 
 
    <button ng-click="addBadElement()"> 
 
     Add Bad Country 
 
    </button> 
 
    <div ng-repeat="country in countries track by country.countryCode"> 
 
     {{country.countryName}} 
 
    </div> 
 
    </div> 
 
</div>

+0

這絕對順便說一下,謝謝!在閱讀和播放更多內容之後,我添加了幾點: 1.在許多情況下,似乎導致變量不明確,特別是在ng-repeat模式中。 2.如果你想使用它,你必須注入$注入器並從中獲取$ rootScope。否則,您將得到一個循環引用 'var $ rootScope = $ injector.get(「$ rootScope」);' 3.無法注入注入了$ rootScope的工廠。 – gush

+0

還添加了2行以獲得通常的控制檯錯誤,從而擴展而不是替換行爲。 (見頂部) – gush