javascript - x = x+1 != x++?

标签 javascript angularjs angularjs-ng-click

我有一个非常简单的 Angular 应用程序。它只是将 ng-click 的值加 1。

参见 JSFiddle

<div ng-app="app" ng-controller="ctrl">
    <div ng-click="hello=hello+1">THIS WORKS ON CLICK: {{hello}}</div>
    <div ng-click="hello++">THIS DOESN'T: {{hello}}</div>
</div>

我希望能够使用 ng-click '++' 变量的值,但显然不能。我做错了什么吗?

最佳答案

ng-click 属性需要一个 Angular 表达式。虽然这些表达式类似于 javascript,但它们不是 javascript,您不能在其中做所有在 javascript 中可以做的事情。有关更多信息,请查看 https://docs.angularjs.org/guide/expression

链接中的重要部分是:

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

Angular expressions do not have access to global variables like window, document or location. This restriction is intentional. It prevents accidental access to the global state – a common source of subtle bugs.

Instead use services like $window and $location in functions called from expressions. Such services provide mockable access to globals.

关于javascript - x = x+1 != x++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24685474/

相关文章:

javascript - 如何解析框架集中页面之间调用的 URL?

javascript - 使用 OpenCV.JS 进行颜色检测

Javascript 提取月、日和年格式

javascript - Angular、Laravel 4 和 MySQL 数据库最佳实践

angularjs - 未找到 Angular $resource 更新方法

javascript - 为什么 "."没有被正则表达式捕获?

javascript - Adobe Creative SDK Web 加载后显示小图像

angularjs - 如何使用 ng-click 提交 Angular js 表单

angularjs - 制作切换 ng-click 事件

javascript - 使用 ng-click 和函数设置变量之间的 Angular 差异