In AngularJS, I wonder how to prevent the elements shown on page before ng-show take effect, I found some posts talk about ng-cloak, but it seems not work in my case, probably the ng-cloak is for prevent double curly bracket rather than Element style.
Another way someone talk about is define some style for before AngularJS initialized, but that is kinda hard to manage.
Is there some official way to handle this?
Use a loader such as this:
JS
CSS
DEMO
http://codepen.io/nicholasabrams/pen/MwOMNR
I just answered a similar topic here: Angularjs tab Loading spinner while rendering
2 alternatives to completely avoid the problem:
ui-router
to keep HTML in separate files, which will be loaded/parsed/injected only when needed and only when the controller is initialized.Here's the hello world tutorial modified to use a separate HTML file:
In case you want to just avoid showing something till it's ready to be shown (some data has been loaded from the backend perhaps) then it's better to use
ng-if
. Ofcourse it works the same withng-show
. But the advantage of usingng-if
is that you delay the creation of the extra DOM until it needs to be shown and as a result you improve the intial page loading time.Here is an example:
Unless you want to show a loader, ng-cloak should be your solution.
Official documentation on ng-cloak
If you still have the issue, you may try to add the css to hide element with ng-cloak inside your html to be sure the browser has it in time.
If you do that, choose on way to add the ng-cloak. For example add it as class:
And add this into your html head tag: