angularjs – 角度ng-if内的按位运算

前端之家收集整理的这篇文章主要介绍了angularjs – 角度ng-if内的按位运算前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在特定视图中为不同的屏幕尺寸加载不同的模板.

标记

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

更多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>

错误

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

这是否意味着在ng-if中不允许按位操作?

解决方法

我遇到了同样的问题,但我刚刚使用过

使用标志枚举添加最终与按位或相同.

猜你在找的Angularjs相关文章