1

I have html block and I want to control it's visibility by Angular variable

<div class={{visibilityCss}}>
    Show/Hide Area
</div>

In angular I have $scope.visibilityCss = 'hide' or $scope.visibilityCss = 'show' which is 'hide' by default. I also have css for .show/.hide classes to toggle the div visibility with display:block/none.

I want this div to be hidden initially but my problem is that while the page is loading the div is visible and only after ~1 sec (when angular js is loaded) it is hidden by javascript. I would like to make it not visible from the very beginning. (something like applying hidden css for .{{visibilityCss}} class if it was a valid css class name)

2 Answers 2

4

To get show/hide feature

<div ng-show="show">
   Area
</div>

In controller define

 $scope.show = true //Shows the Area

or

$scope.show = false //Hides the Area

depending on the requirement.

EDIT

I believe this could help, if further clarification is needed post a Plnkr.

NOT TESTED

var app = angular.module('app', []);

app.run(function($rootScope) { 
  $rootScope.show = false;
});

app.controller('Ctrl', function($rootScope,$scope) { 
   $rootScope.show = true 
})
Sign up to request clarification or add additional context in comments.

2 Comments

Agreed that this is a better approach and is clearer in the html.
Good to know but it doesn't solve the problem for page loading.
2

Okay, first up your HTML code is bad. You should be using ng-class instead:

<div ng-class="visibilityCss">
  Show/Hide Area
</div>

To answer your question, Angular has a built in setup for exactly this feature. By adding the class ng-cloak, and adding the following CSS to your stylesheet, it'll be hidden when the page loads. Angular will then show it once everything is loaded.

[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
  display: none !important;
}

And then in your HTML:

<div class="ng-cloak" ng-class="visibilityCss">
  Show/Hide Area
</div>

Edit As mentioned by Chandresh, using the ng-show directive with a true/false value would be a better option than using ng-class

6 Comments

alternatively you can put your angular script tag in the <head> of the page, then the page won't load at all until angular is already loaded.
@EdHinchliffe That also stifles page loading.
It does indeed, which I eluded to. My angular apps are very little without angular (most of the content is inside a ui-view), so I really don't mind if the page doesn't start to load until angular has - it makes very little difference, and is much easier than messing around with ng-cloak.
I need to apply some dynamic styles as well when it is visible so I think this solution will fit better than combining class with hide/show. Let me try
@jonasnas you need at least this solution, semantically it also makes sense to have ng-class and ng-show on the element, it also decouples your logic from your display a bit more.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.