我有一个非常简单的 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/