简体   繁体   English

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

[英]Using & (bitwise AND operator) in Angular ng-if expressions

I can't get the & operator to work in an Angular ng-if expression (to use with some bit flags).我无法让&运算符在 Angular ng-if表达式中工作(与某些位标志一起使用)。 Suppose we have some HTML like this:假设我们有一些这样的 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,因此是一个真值。

However, Angular throws a Syntax Error exception every time.但是,Angular 每次都会抛出Syntax Error异常。 Is the operation not allowed?不允许操作吗? Or am I doing something wrong?还是我做错了什么?

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使用它(显然不使用该函数)?

You cannot use the bitwise & operator in an Angular expression.您不能在 Angular 表达式中使用按位&运算符。 According to the documentation :根据文档

Angular expressions are like JavaScript expressions with the following differences: Angular 表达式类似于 JavaScript 表达式,但有以下区别:

  • ... ...
  • You cannot use Bitwise, , or void operators in an Angular expression.你不能使用按位, ,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. &&逻辑与运算符。

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 的幂)相当于value % (2 * x) >= x

One can accomplish this with a bitwise pipe:可以使用按位管道完成此操作:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'hasFlags'
})
export class HasFlagsPipe implements PipeTransform {

  transform(flagsL: number, flagsR: number): boolean {
    return (flagsL & flagsR) != 0;
  }

}

Usage:用法:

<div [hidden]="flagsL | hasFlags : flagsR">

Note: I'm using angular 2+ but came across this post so hopefully this helps someone else.注意:我使用的是 angular 2+,但遇到了这篇文章,所以希望这对其他人有帮助。

The problem is that you're using & instead of && .问题是您使用的是&而不是&& & is the bitwise operator, and Angular doesn't allow bitwise operators in expressions. &是按位运算符,而 Angular 不允许在表达式中使用按位运算符。

I am not sure what you want to accomplish here but you are using a bit-wise AND operator.我不确定您想在这里完成什么,但您正在使用按位 AND 运算符。 And in the angular documentation it says:在角度文档中,它说:

No Bitwise, Comma, And Void Operators: You cannot use Bitwise, , or void operators in an Angular expression.无按位、逗号和空运算符:您不能在 Angular 表达式中使用按位、 或 void 运算符。

AngularJS expression AngularJS 表达式

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!看到 plunkr!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM