Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). Suppose we have some HTML like this:

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

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

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

Link to the 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)?

like image 230
illeb Avatar asked Sep 02 '16 14:09

illeb


People also ask

What is the synonym of using?

The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end.

What is the verb of using?

verb (used with object), used, us·ing. to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided.

What does it mean by using someone?

transitive verb. If you say that someone uses people, you disapprove of them because they make others do things for them in order to benefit or gain some advantage from it, and not because they care about the other people. [disapproval]

What is the word for using what you have?

The words employ and use are common synonyms of utilize.


2 Answers

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

Angular expressions are like JavaScript expressions with the following differences:

  • ...
  • You cannot use Bitwise, , or void operators in an Angular expression.

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

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:

<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.

like image 168
Michael Liu Avatar answered Sep 27 '22 18:09

Michael Liu


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.

like image 41
Jay Avatar answered Sep 27 '22 16:09

Jay