gpt4 book ai didi

angularjs ng-click 默默地吃错误

转载 作者:行者123 更新时间:2023-12-03 12:52:28 26 4
gpt4 key购买 nike

如果我有这样的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com