本文介绍了angular ng-if 中的按位运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定视图中为不同的屏幕尺寸加载不同的模板.

标记:

<div id="phoneMetaDiv" class="visible-xs"><!-- 在手机上可见--></div><div id="tabletMetaDiv" class="visible-sm"><!-- 在平板电脑上可见--></div><div id="desktopMetaDiv" class="visible-md"><!-- 在桌面上可见--></div><div id="giantScrMetaDiv" class="visible-lg"><!-- 在大屏幕上可见--></div>

应用脚本:

app.run(function ($rootScope) {$rootScope.Views = {电话 : 1,//0001平板电脑 : 2,//0010桌面 : 4,//0100GiantScr: 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;别的抛出无效视图";$rootScope.View = 函数(值){return ($rootScope.CurrentView & value) !=0;};}

更多 HTML

包括模板 1...<div><div ng-if="View(Views.Phone)">包括模板 2... <div>

错误:

错误:[$parse:syntax] 语法错误:标记|"出乎意料,期望 [)] 位于表达式 [View(Views.Desktop | Views.GiantScr)] 的第 20 列,开始于 [|Views.GiantScr)].

这是否意味着 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?

解决方案

I had the same problem, but I just used +

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

这篇关于angular ng-if 中的按位运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:05