Mehrad Sadegh
Mehrad Sadegh

Reputation: 1134

Bitwise operation inside angular ng-if

I want to load different templates for different screen sizes in a particular view.

Markup:

<div id="phoneMetaDiv"      class="visible-xs"><!-- Visible on phones  --></div>
<div id="tabletMetaDiv"     class="visible-sm"><!-- Visible on tablets  --></div>
<div id="desktopMetaDiv"    class="visible-md"><!-- Visible on desktops --></div>
<div id="giantScrMetaDiv"   class="visible-lg"><!-- Visible on giant screens --></div>

App script:

app.run(function ($rootScope) {


    $rootScope.Views = {
            Phone   : 1, //0001
            Tablet  : 2, //0010
            Desktop : 4, //0100
            GiantScr: 8, //1000
    };

    if($("#giantScrMetaDiv").is(":visible"))
        $rootScope.CurrentView = $rootScope.Views.GiantScr;
    else if($("#desktopMetaDiv").is(":visible"))
        $rootScope.CurrentView = $rootScope.Views.Desktop;
    else if($("#tabletMetaDiv").is(":visible"))
        $rootScope.CurrentView = $rootScope.Views.Tablet;
    else if($("#phoneMetaDiv").is(":visible"))
        $rootScope.CurrentView = $rootScope.Views.Phone;
    else
        throw "invalid view";

    $rootScope.View = function (value) {
        return ($rootScope.CurrentView & value) !=0;
    };
}

More HTML

<div ng-if="View(Views.Desktop | Views.GiantScr)"> Include for template 1... <div>
<div ng-if="View(Views.Phone)"> Include for template 2... <div>

And the error:

Error: [$parse:syntax] Syntax Error: Token '|' is unexpected, expecting [)] at column 20 of the expression [View(Views.Desktop | Views.GiantScr)] starting at [| Views.GiantScr)].

Does this mean bitwise operations are not allowed inside ng-if?

Upvotes: 4

Views: 3634

Answers (2)

Evan Kaiser
Evan Kaiser

Reputation: 181

I had the same problem, but I just used +

With flag enums addition ends up being the same as bitwise or.

Upvotes: 2

mpm
mpm

Reputation: 20155

"|" is used for filters , so I guess not, just write a function.and Angular expression language is not javascript.There are a lot of differences.

http://docs.angularjs.org/guide/expression

You'll have to write a filter or a function like "bitwise-or"

Upvotes: 3

Related Questions