Angularjs – ng-cloak/ng-show elements blink

Though the documentation doesn’t mention it, it might not be enough to add the display: none; rule to your CSS. In cases where you are loading angular.js in the body or templates aren’t compiled soon enough, use the ng-cloak directive and include the following in your CSS:

/* 
  Allow angular.js to be loaded in body, hiding cloaked elements until 
  templates compile.  The !important is important given that there may be 
  other selectors that are more specific or come later and might alter display.  
 */
[ng\:cloak], [ng-cloak], .ng-cloak {
  display: none !important;
}

As mentioned in the comment, the !important is important. For example, if you have the following markup

<ul class="nav">
  <li><a href="https://stackoverflow.com/foo" ng-cloak>{{bar}}</a></li>
</ul>

and you happen to be using bootstrap.css, the following selector is more specific for your ng-cloak‘ed element

.nav > li > a {
  display: block;
}

So if you include a rule with simply display: none;, Bootstrap’s rule will take precedence and the display will be set to block, so you’ll see the flicker before the template compiles.

Leave a Comment