揭露angular.js JavaScript错误 [英] Expose javascript errors in angular.js

查看:106
本文介绍了揭露angular.js JavaScript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,角发动机不输出原始的的JavaScript 错误。例如:

in some cases, the angular engine doesn't output the raw javascript error. For example

myapp.directive('helloWorld', function() {
return {
    scope: '@',
    restrict: 'AE',
    template: '<p ng-click="clearMessage2()">Hello, World! {{message}}</p>',
    link: function(scope, elem, attrs) {
       scope.clearMessage = function() {
          scope.message = '';
       }
   }
}});

当我点击 P 制作了指令,我期待在控制台中的错误说元素 clearMessage2()没有定义,但是这并没有发生,检查事物的唯一方法是使用clearMessage定义中的console.log。

When I click on p element produced with directive I'm expecting the error in console to say clearMessage2() is not defined, but this doesn't happen and the only way to check things is to use console.log inside clearMessage definition.

是否有可能改变angular.js的这种行为,不隐瞒JS code内部发生错误?

Is it possible to change that behavior of angular.js and do not hide errors happening inside JS code?

推荐答案

这是可能的,但不推荐。问题是,角不会在NG-点击指令执行的方法是(像普通的onclick),而是通过评估在 $解析服务前pressions。从角DOC:

It is possible, but not recommended. The problem is that angular does not execute the method in the ng-click directive as is (like in regular onclick), but instead evaluates expressions using the $parse service. From the angular doc:

角不使用JavaScript的eval(),以评估前pressions。
  相反角度的解析$服务处理这些前pressions。

Angular does not use JavaScript's eval() to evaluate expressions. Instead Angular's $parse service processes these expressions.

EX pression评价的角度实施故意宽容。

The implementation of expression evaluation in Angular is deliberately forgiving.

在JavaScript中,试图评估未定义的属性产生的ReferenceError或类型错误。在棱角分明,前pression评价是宽容到undefined和null。它更有意义,显示无非是为了抛出一个异常,如果是不确定的(也许我们都在等待服务器的响应,它会很快成为定义)。如果ex pression评价是不是原谅我们不得不编写杂乱code绑定

In JavaScript, trying to evaluate undefined properties generates ReferenceError or TypeError. In Angular, expression evaluation is forgiving to undefined and null. It makes more sense to show nothing than to throw an exception if a is undefined (perhaps we are waiting for the server response, and it will become defined soon). If expression evaluation wasn't forgiving we'd have to write bindings that clutter the code

所以$ parseProvider不会执行未定义的功能可言,相反,它会执行空操作的功能(这是一个空对象模式的实现)。这里是从 $ parseFunctionCall 方式的摘录:

So the $parseProvider will not execute an undefined function at all, instead it will execute a noop function (which is an implementation of a null object pattern). Here is an excerpt from the $parseFunctionCall method:

var fn = fnGetter(scope, locals, context) || noop;

null对象的执行会做什么,这是发生了什么事。你也许可以实现通过修改$ parseFunctionCall执行任意功能,而不是执行一个空操作函数想要的东西。

The execution of a null object will do nothing, and that is what's happening. You could probably achieve what you want by modifying the $parseFunctionCall to execute any function, instead of executing a noop function.

更改code看起来就像是唯一的选择,因为这些服务的配置是不够的你的用例。但是,我不认为这是一个推荐的方法,除非你知道角API非常好。

Changing code looks like the only option since configuration of these services is not sufficient for your use-case. However, i don't think it's a recommended approach unless you know the Angular API very well.

有关更多阅读:

角前pressions

$解析服务

这篇关于揭露angular.js JavaScript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆