angularjs ng-click 默默地吃错误 [英] angularjs ng-click silently eats errors

查看:39
本文介绍了angularjs ng-click 默默地吃错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的 ng-click:ng-click="buggy()"并点击控制台上没有生成错误信息.

If I have an ng-click like this: ng-click="buggy()" and click on no error message is generated on the console.

这使得调试有点棘手.

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

Why aren't error messages generated? Anything I can do?

推荐答案

Angular 表达式

实际上,ng-click 并没有什么特别之处,这是角度表达式的默认行为.

Angular expressions

Actually, It's not something special with ng-click, It's the default behavior of angular expressions .

buggy() 不使用常规 javascript 进行评估.它用 $parse 求值.

buggy() is not evaluated with regular javascript. It is evaluated with $parse.

$parse 计算表达式并返回一个针对作用域运行的函数.

$parse evaluates expressions and returns a function that would run against a scope.

$parse 仅在表达式无效时记录错误.

$parse only log errors when the expression is not valid.

Angular 表达式评估是宽容的,我想不出任何方法来传递它.

Angular expression evaluation is forgiving , I cannot think of any way to pass it.

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

A rationale for Angular expressions to be forgiving of undefined and null has to do with data binding. Bound variables may initially be undefined or null when they are compiled into the DOM- one really clear example is when the bound variable is dependent on a promise. The Angular team decided that rather than having error messages popping up until that promise is resolved it'd be better to continue silently.

来自Angular 表达式指南:

如果 a 是,则不显示比抛出异常更有意义未定义(也许我们正在等待服务器响应,它会很快就会被定义).如果表达式评估不宽容,我们会必须编写使代码混乱的绑定,例如:{{((a||{}).b||{}).c}}

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/#!主题/角度/HRVOUKEHLFw

表达式是类似于 JavaScript 的代码片段,通常放置在诸如 {{ expression }} 之类的绑定中.表达式由 $parse 服务处理.表达式通常使用过滤器进行后处理,以创建更用户友好的格式.

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 视图表达式视为 JavaScript 表达式可能很诱人,但这并不完全正确,因为 Angular 不使用 JavaScript eval() 来计算表达式.您可以将 Angular 表达式视为具有以下差异的 JavaScript 表达式:

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:

  • 属性评估:所有属性的评估都是针对执行评估的范围,这与 JavaScript 中的表达式是针对全局窗口评估的不同.

  • 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:表达式求值对 undefined 和 null 是宽容的,这与 JavaScript 不同,在 JavaScript 中尝试求值 undefined 属性会产生 ReferenceError 或 TypeError.

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 默默地吃错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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