Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise Angular Expression not working

I have a basic ng-show expression as follows:

ng-show="((message.status & messageStatus.Spam) != 0)"

However, this fails with the following msg: "Token '&' is unexpected, expecting [)] at column 18 of the expression".

Does Angular support bitwise operations, or do I need to write a function to evaluate something as simple as that?

like image 883
Val Avatar asked Aug 02 '13 20:08

Val


People also ask

How does Bitwise not work?

The Bitwise Not operation treats the sign bit as it would any other bit. If the input for a pixel location is negative, the output is negative; if the input is positive, the output is positive. If the input is a multiband raster, the output will be a multiband raster.

How do I find Bitwise not?

Bitwise NOT (~)Each bit in the operand is inverted in the result. The 32-bit signed integer operand is inverted according to two's complement. That is, the presence of the most significant bit is used to express negative integers. Bitwise NOTing any number x yields -(x + 1) .

Which of the following is called Bitwise Not Operator?

These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits. Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).


2 Answers

From the angularjs github buglist: https://github.com/angular/angular.js/issues/2838

http://docs.angularjs.org/guide/expression "Angular Expressions vs. JS Expressions It might be tempting to think of Angular view expressions as JavaScript expressions, but that is not entirely correct, since Angular does not use a JavaScript eval() to evaluate expressions."

You can use a filter to achieve the effect as such:

angular.module('project', [])
.filter('bitwiseAnd', function () {
    return function (firstNumber, secondNumber) {
        return ((parseInt(firstNumber, 10) & parseInt(secondNumber, 10)) === parseInt(secondNumber, 10));
    // return firstNumber % secondNumber > 0
    };
});
like image 134
Matt Smith Avatar answered Oct 21 '22 13:10

Matt Smith


It doesn't support according to the doc.

Thought it mentioned '|' on the doc but don't get confused, '|' is not bitwise OR it is Angularjs's filter.

like image 42
zs2020 Avatar answered Oct 21 '22 14:10

zs2020