Angular ng If not working with razor syntax

2019-08-10 23:07发布

问题:

I am using Angular Js with Razor.

@{
    var pageMode = (string) ViewBag.PageMode;
}

<div ng-if="@pageMode == 'answer'">
   <h2>Greetings</h2>
</div>

Value is in ViewBag.PageMode is "answer", still the ng if does not render. i.e "Greetings" message does not work.

What am I doing wrong here... ?

回答1:

you cant use var pageMode variable inside directives like ng-if. you need to use $scope variable.

so, simplest thing u can do is,

@{
    var pageMode = (string) ViewBag.PageMode;
}

<input type="hidden" ng-model="pageMode" ng-init="pageMode = '@pageMode'">


<div ng-if="pageMode == 'answer'">