angularjs ng-click 默默地吃错误

标签 angularjs angularjs-ng-click

如果我有这样的 ng-click:
ng-click="buggy()"
并单击控制台上不会生成任何错误消息。

这使得调试有点棘手。

为什么不生成错误消息?有什么我能做的吗?

最佳答案

Angular 表达式

实际上,ng-click 并没有什么特别之处。 , 这是 的默认行为 Angular 表达式 .
buggy()不使用常规 javascript 进行评估。它使用 $parse 进行评估.
$parse计算表达式并返回一个针对范围运行的函数。
$parse仅在表达式无效时记录错误。

Angular 表达式评估是宽容的,我想不出任何方法来通过它。

为什么表情是宽容的?

Angular 表达式允许 undefined 和 null 的基本原理与数据绑定(bind)有关。当绑定(bind)变量被编译到 DOM 中时,它们最初可能是未定义的或为空的——一个非常明显的例子是绑定(bind)变量依赖于一个 Promise。 Angular 团队决定,与其在 promise 解决之前弹出错误消息,不如默默地继续。

来自 Angular guide to expressions :

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, for example: {{((a||{}).b||{}).c}}



另见:https://groups.google.com/forum/m/#!topic/angular/HRVOUKEHLFw

Angular expressions :

Expressions are JavaScript-like code snippets that are usually placed in bindings such as {{ expression }}. Expressions are processed by the $parse service. Expressions are often post processed using filters to create a more user-friendly format.

Angular Expressions vs. JS Expressions

It might be tempting to think of Angular view expressions as JavaScript expressions, but that is not entirely correct, since Angular does not use a JavaScript eval() to evaluate expressions. You can think of Angular expressions as JavaScript expressions with following differences:

  • Attribute Evaluation: evaluation of all properties are against the scope doing the evaluation, unlike in JavaScript where the expressions are evaluated against the global window.

  • Forgiving: expression evaluation is forgiving to undefined and null, unlike in JavaScript, where trying to evaluate undefined properties can generate ReferenceError or TypeError.

  • No Control Flow Statements: you cannot do any of the following in angular expression: conditionals, loops, or throw.

关于angularjs ng-click 默默地吃错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21297108/

相关文章:

javascript - 防止滑动触发 ng-click 处理程序

javascript - 错误 : ng:areq Bad Argument: Argument 'registerController' is not a function, 未定义

jquery - 关注 ng-repeat 生成的输入

javascript - 将匿名函数中的 javascript 与 gulp 连接起来

javascript - Angularjs 双向数据绑定(bind)不起作用?

angularjs - 如何将参数传递给 ng-click 函数?

angularjs - 可以将 ngclick 值传递给函数吗?

angularjs - 具有服务依赖性的 Angular 全局错误处理程序

angularjs - 在 AngularJS 模型更改后更新 MathJax

javascript - Angularjs ng-click() 不是从 ng-repeat 指令触发