在Angular ng-if表达式中使用&(按位AND运算符)

时间:2022-10-23 08:12:44

I can't get the & operator to work in an Angular ng-if expression (to use with some bit flags). Suppose we have some HTML like this:

我无法使&运算符在Angular ng-if表达式中工作(与某些位标志一起使用)。假设我们有这样的HTML:

<div ng-if="value & 2"> </div>

If value equals 3, then the bitwise operation should return 2 and thus a true value.

如果value等于3,则按位运算应返回2,因此返回true值。

However, Angular throws a Syntax Error exception every time. Is the operation not allowed? Or am I doing something wrong?

但是,Angular每次都会抛出一个语法错误异常。操作不被允许吗?或者我做错了什么?

Link to the plunker.

链接到plunker。

Edit: I already resolved my issue by using a simple function that does the job:

编辑:我已经通过使用一个完成工作的简单函数解决了我的问题:

$scope.checkFlag = function(value, flag){
   return value & flag;
}

But I really don't like this solution. Is there a way to use it in an ng-if (without using the function, obviously)?

但我真的不喜欢这个解决方案。有没有办法在ng-if中使用它(显然不使用该功能)?

4 个解决方案

#1


5  

You cannot use the bitwise & operator in an Angular expression. According to the documentation:

您不能在Angular表达式中使用bitwise&运算符。根据文件:

Angular expressions are like JavaScript expressions with the following differences:

Angular表达式就像JavaScript表达式,但有以下不同之处:

  • ...
  • You cannot use Bitwise, , or void operators in an Angular expression.
  • 您不能在Angular表达式中使用Bitwise,或void运算符。

If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.

如果要运行更复杂的JavaScript代码,则应将其设置为控制器方法并从视图中调用该方法。

A note on nomenclature: & is the bitwise AND operator; && is the logical AND operator.

关于命名法的注释:&是按位AND运算符; &&是逻辑AND运算符。

UPDATE: Your checkFlag function is probably the best workaround because its name makes it clear what it does (and that's what I would use), but if you absolutely don't want an extra function, you can use the following equivalent expression:

更新:你的checkFlag函数可能是最好的解决方法,因为它的名字清楚地表明它的作用(这就是我将要使用的),但是如果你绝对不想要额外的函数,你可以使用以下等效表达式:

<div ng-if="value % 4 >= 2"> </div>

In general, (value & x) != 0 (where x is a power of 2) is equivalent to value % (2 * x) >= x.

通常,(value&x)!= 0(其中x是2的幂)等于值%(2 * x)> = x。

#2


2  

The problem is that you're using & instead of &&. & is the bitwise operator, and Angular doesn't allow bitwise operators in expressions.

问题是你正在使用&而不是&&。 &是按位运算符,Angular不允许表达式中的按位运算符。

#3


1  

I am not sure what you want to accomplish here but you are using a bit-wise AND operator. And in the angular documentation it says:

我不确定你想要在这里完成什么,但你正在使用一个按位运算符。在角度文档中,它说:

No Bitwise, Comma, And Void Operators: You cannot use Bitwise, , or void operators in an Angular expression.

No Bitwise,Comma和Void Operators:您不能在Angular表达式中使用Bitwise,或void运算符。

AngularJS expression

#4


1  

Since no one has provided a solution I'd assume you could just modify your html to:

由于没有人提供解决方案,我认为你可以将你的html修改为:

<body ng-controller="MainCtrl">
   <div ng-if="check(value,2)">Hey! </div>
</body>

with

$scope.check = function(val, providedInt){
   return val & providedInt;
 };

See plunkr!

#1


5  

You cannot use the bitwise & operator in an Angular expression. According to the documentation:

您不能在Angular表达式中使用bitwise&运算符。根据文件:

Angular expressions are like JavaScript expressions with the following differences:

Angular表达式就像JavaScript表达式,但有以下不同之处:

  • ...
  • You cannot use Bitwise, , or void operators in an Angular expression.
  • 您不能在Angular表达式中使用Bitwise,或void运算符。

If you want to run more complex JavaScript code, you should make it a controller method and call the method from your view.

如果要运行更复杂的JavaScript代码,则应将其设置为控制器方法并从视图中调用该方法。

A note on nomenclature: & is the bitwise AND operator; && is the logical AND operator.

关于命名法的注释:&是按位AND运算符; &&是逻辑AND运算符。

UPDATE: Your checkFlag function is probably the best workaround because its name makes it clear what it does (and that's what I would use), but if you absolutely don't want an extra function, you can use the following equivalent expression:

更新:你的checkFlag函数可能是最好的解决方法,因为它的名字清楚地表明它的作用(这就是我将要使用的),但是如果你绝对不想要额外的函数,你可以使用以下等效表达式:

<div ng-if="value % 4 >= 2"> </div>

In general, (value & x) != 0 (where x is a power of 2) is equivalent to value % (2 * x) >= x.

通常,(value&x)!= 0(其中x是2的幂)等于值%(2 * x)> = x。

#2


2  

The problem is that you're using & instead of &&. & is the bitwise operator, and Angular doesn't allow bitwise operators in expressions.

问题是你正在使用&而不是&&。 &是按位运算符,Angular不允许表达式中的按位运算符。

#3


1  

I am not sure what you want to accomplish here but you are using a bit-wise AND operator. And in the angular documentation it says:

我不确定你想要在这里完成什么,但你正在使用一个按位运算符。在角度文档中,它说:

No Bitwise, Comma, And Void Operators: You cannot use Bitwise, , or void operators in an Angular expression.

No Bitwise,Comma和Void Operators:您不能在Angular表达式中使用Bitwise,或void运算符。

AngularJS expression

#4


1  

Since no one has provided a solution I'd assume you could just modify your html to:

由于没有人提供解决方案,我认为你可以将你的html修改为:

<body ng-controller="MainCtrl">
   <div ng-if="check(value,2)">Hey! </div>
</body>

with

$scope.check = function(val, providedInt){
   return val & providedInt;
 };

See plunkr!