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.在许多情况下,似乎导致变量不明确,特别是在ng-repeat模式中。 2.如果你想使用它,你必须注入$注入器并从中获取$ rootScope。否则,您将得到一个循环引用 'var $ rootScope = $ injector.get(“$ rootScope”);' 3.无法注入注入了$ rootScope的工厂。 – gush
还添加了2行以获得通常的控制台错误,从而扩展而不是替换行为。 (见顶部) – gush