Hide a div if viewbag is false

1,111 views
Skip to first unread message

Marina L

unread,
Aug 1, 2016, 4:40:50 AM8/1/16
to AngularJS
Hello,

I use Angular js in my MVC app.

I need to hide a div when my viewbag is false

I have try this

 public ActionResult Index()
       
{
           
ViewBag.total = false;
           
return View();
       
}

My View
 
@{
           
var total= (string) ViewBag.total;
         
}
       
<div ng-if="total">
content here
       
</div>

and also this from here http://stackoverflow.com/questions/27673786/angular-ng-if-not-working-with-razor-syntax

but it doesn't work. Any idea?

thank you

hirdesh tomar

unread,
Aug 1, 2016, 11:13:23 PM8/1/16
to ang...@googlegroups.com, mly...@gmail.com

Hi Marina,

Not sure about this approach but you can use "ng-show" directive in angular js to hide or unhide a div.

In your controller define a flag on scope like this

$scope.viewBagFlag = { hideShow : true };

and set $scope.viewBagFlag to false when your "viewBag" variable becomes false.

In your html use this flag to show or hide that div.

<div ng-show="viewBagFlag.hideShow">
</div>

Hope this helps!

Thanks,
Hirdesh


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Marina L

unread,
Aug 2, 2016, 2:33:26 AM8/2/16
to hirdesh tomar, ang...@googlegroups.com
hi,
thank you for your reply. How can I pass the ViewBag  (true or false) value from my mvc controller to angular controller?

hirdesh tomar

unread,
Aug 2, 2016, 3:19:12 AM8/2/16
to Marina L, ang...@googlegroups.com

If your controller is also written in angular js then why not use same controller for that <div> page? In case there is a necessity to use a separated controller then you may have to write an angular service to pass values between controllers. There are other options as well like using $localStorage angular service or by using rootScope variable.

Thanks,
Hirdesh

Marina L

unread,
Aug 2, 2016, 4:28:08 AM8/2/16
to hirdesh tomar, ang...@googlegroups.com
Its my MVC Controller. It written in C and I execute my queries there. So I need if my query is null then I turn the ViewBag to false. If has values I turn my ViewBag to true.

Marina L

unread,
Aug 2, 2016, 4:31:48 AM8/2/16
to hirdesh tomar, ang...@googlegroups.com
The problem is how to pass the value of the Viewbag in my angular js

Ali Al Lawati

unread,
Aug 2, 2016, 9:01:38 AM8/2/16
to AngularJS
Hi there Marina L


Do this

<div ng-if="@ViewBag.total">
content here
       
</div>

Marina L

unread,
Aug 3, 2016, 1:33:35 AM8/3/16
to ang...@googlegroups.com
Hello,

thank you for your reply

The

<div ng-if="@ViewBag.total"

doesn't work for me. I notice that I cannnot even call my ViewBag variable. Lets say that I have a viewbag in my mvc controller like that 

 ViewBag.total = "hello" 

and then in my View I do this

@ViewBag.total

It doesn't display nothing. Is there any conflict between razor syntax and angularjs?

thank you



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/wo8R1zVlq-U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

Marina L

unread,
Aug 4, 2016, 3:34:17 AM8/4/16
to ang...@googlegroups.com
Hello to all,

I finally dropped the idea of viewbag and solved the problem like that

<div ng-hide="myarray[0].myfield== 0 ">Content</div>

Thank you all for the replies
Reply all
Reply to author
Forward
0 new messages