为什么我不能在 AngularJS 的 `ng-mousemove` 中使用前置自增运算符?
Why can not I use the pre-increment operator in the `ng-mousemove` in AngularJS?
为什么我不能在AngularJS中的ng-mousemove
中使用前置自增运算符?
这是我的代码:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-mousemove="++count;">Mouse Over Me!</h1>
<h2>{{ count }}</h2>
</body>
count
不递增。但是一旦我将代码更改为:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-mousemove="count = count + 1;">Mouse Over Me!</h1>
<h2>{{ count }}</h2>
</body>
然后代码就可以正常工作了。为什么 AngularJS 不允许 ++count;
,但允许 count = count + 1;
?
ng-mouseover
指令计算 AngularJS 表达式,它们类似于 JavaScript 但有区别。
来自文档:
AngularJS Expressions vs. JavaScript Expressions
AngularJS expressions are like JavaScript expressions with the following differences:
Context: JavaScript expressions are evaluated against the global window. In AngularJS, expressions are evaluated against a scope object.
Forgiving: In JavaScript, trying to evaluate undefined properties generates ReferenceError or TypeError. In AngularJS, expression evaluation is forgiving to undefined
and null
.
Filters: You can use filters within expressions to format data before displaying it.
No Control Flow Statements: You cannot use the following in an AngularJS expression: conditionals, loops, or exceptions.
No Function Declarations: You cannot declare functions in an AngularJS expression, even inside ng-init directive.
No RegExp Creation With Literal Notation: You cannot create regular expressions in an AngularJS expression. An exception to this rule is ng-pattern
which accepts valid RegExp.
No Object Creation With New Operator: You cannot use new
operator in an AngularJS expression.
No Bitwise, Comma, And Void Operators: You cannot use Bitwise, ,
or void
operators in an AngularJS expression.
注意:这不是差异的完整列表。
在幕后,AngularJS 框架有自己的 expression parser written in JavaScript which avoids the enormous security risk of using eval()
. If you want to eval()
an AngularJS expression yourself, use the $rootScope/$scope $eval()
method or the $parse Service。
如示例所示,AngularJS 表达式解析器不解析递增 (++
) 运算符或递减 (--
) 运算符。
AngularJS支持的运算符有:1
+ - * / % === !== == != < > <= >= && || ! = |
来自文档:
Sandbox removal
Each version of AngularJS 1 up to, but not including 1.6, contained an expression sandbox, which reduced the surface area of the vulnerability but never removed it. In AngularJS 1.6 we removed this sandbox as developers kept relying upon it as a security feature even though it was always possible to access arbitrary JavaScript code if one could control the AngularJS templates or expressions of applications.
有关详细信息,请参阅
为什么我不能在AngularJS中的ng-mousemove
中使用前置自增运算符?
这是我的代码:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-mousemove="++count;">Mouse Over Me!</h1>
<h2>{{ count }}</h2>
</body>
count
不递增。但是一旦我将代码更改为:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-mousemove="count = count + 1;">Mouse Over Me!</h1>
<h2>{{ count }}</h2>
</body>
然后代码就可以正常工作了。为什么 AngularJS 不允许 ++count;
,但允许 count = count + 1;
?
ng-mouseover
指令计算 AngularJS 表达式,它们类似于 JavaScript 但有区别。
来自文档:
AngularJS Expressions vs. JavaScript Expressions
AngularJS expressions are like JavaScript expressions with the following differences:
Context: JavaScript expressions are evaluated against the global window. In AngularJS, expressions are evaluated against a scope object.
Forgiving: In JavaScript, trying to evaluate undefined properties generates ReferenceError or TypeError. In AngularJS, expression evaluation is forgiving to
undefined
andnull
.Filters: You can use filters within expressions to format data before displaying it.
No Control Flow Statements: You cannot use the following in an AngularJS expression: conditionals, loops, or exceptions.
No Function Declarations: You cannot declare functions in an AngularJS expression, even inside ng-init directive.
No RegExp Creation With Literal Notation: You cannot create regular expressions in an AngularJS expression. An exception to this rule is
ng-pattern
which accepts valid RegExp.No Object Creation With New Operator: You cannot use
new
operator in an AngularJS expression.No Bitwise, Comma, And Void Operators: You cannot use Bitwise,
,
orvoid
operators in an AngularJS expression.
注意:这不是差异的完整列表。
在幕后,AngularJS 框架有自己的 expression parser written in JavaScript which avoids the enormous security risk of using eval()
. If you want to eval()
an AngularJS expression yourself, use the $rootScope/$scope $eval()
method or the $parse Service。
如示例所示,AngularJS 表达式解析器不解析递增 (++
) 运算符或递减 (--
) 运算符。
AngularJS支持的运算符有:1
+ - * / % === !== == != < > <= >= && || ! = |
来自文档:
Sandbox removal
Each version of AngularJS 1 up to, but not including 1.6, contained an expression sandbox, which reduced the surface area of the vulnerability but never removed it. In AngularJS 1.6 we removed this sandbox as developers kept relying upon it as a security feature even though it was always possible to access arbitrary JavaScript code if one could control the AngularJS templates or expressions of applications.
有关详细信息,请参阅