简体   繁体   English

调试:ng-click和不存在的函数

[英]Debugging: ng-click and non-existent functions

Before fixing the problem I had an element (inside an HTML template) with a ng-click which was calling an non-existent function. 在解决问题之前,我有一个元素(在HTML模板中),其中有一个ng-click,它调用了一个不存在的函数。

Is there a way to enable a strict mode (like use strict in JS) or something similar to see this kind of problems in the console? 有没有办法启用严格模式(如在JS中使用严格)或类似的东西在控制台中看到这种问题?

UPDATE: I tried also with $compileProvider.debugInfoEnabled(true) without success 更新:我也试过$ compileProvider.debugInfoEnabled(true)但没有成功

Short Answer: There is no option to do this in vanilla AngularJS, however it can be done with a hack. 简答:没有选择在vanilla AngularJS中做到这一点,但是可以通过hack来完成。

Long Answer: The expressions you use in DOM event handling directives (such as ng-click, ng-keydown, and ng-submit) are compiled by Angular's $parse service. 长答案:您在DOM事件处理指令中使用的表达式(例如ng-click,ng-keydown和ng-submit)由Angular的$ parse服务编译。 You can see where this happens inside the code for the event directive . 您可以在事件指令代码中看到这发生的位置。 The $parse service includes its own implementation of a lexer and parser, which compile the expression down into JavaScript. $ parse服务包括它自己的词法分析器和解析器的实现,它将表达式编译成JavaScript。

The generated JavaScript has a bunch of safety checks to prevent things from happening, such as errors being thrown from use of undefined functions. 生成的JavaScript有一堆安全检查来防止事情发生,例如使用未定义函数引发的错误。 A button with this code: 带有此代码的按钮:

<button ng-click="handleClick()">Click Me!<button>

Will end up generating JavaScript for the event handler that does something similar to this: 最终将为事件处理程序生成JavaScript,它执行类似于此的操作:

if ($scope.handleClick != null) {
    return $scope.handleClick();
} else {
    return undefined;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM