On my page I have a div inside ng-if when there is a number of scripts to load ,i.e when the page is not fully loaded.The div is visible. How do I hide it unless page/angular is fully loaded.
<div id = "menu" ng-if="someCondition" >
<ul class="user-menu">
<li>
menu item 1
</li>
<li>
menu item 2
</li>
</ul>
</div>
NgIflink. A structural directive that conditionally includes a template based on the value of an expression coerced to Boolean. When the expression evaluates to true, Angular renders the template provided in a then clause, and when false or null, Angular renders the template provided in an optional else clause.
ngIf is the directive. Because it's a structural directive (template-based), you need to use the * prefix to use it into templates. *ngIf corresponds to the shortcut for the following syntax (“syntactic sugar”): <template [ngIf]="condition">
You can use data-ng-, instead of ng-, if you want to make your page HTML valid. You will learn a lot more about directives later in this tutorial.
The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.
Take a look at the documentation for ng-cloak.
From the documentation:
The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.
Also:
The directive can be applied to the element, but the preferred usage is to apply multiple ngCloak directives to small portions of the page to permit progressive rendering of the browser view.
<div id = "menu" ng-if="someCondition" ng-cloak>
<ul class="user-menu">
<li>
menu item 1
</li>
<li>
menu item 2
</li>
</ul>
</div>
How it works:
The following styles are applied to the document (the angular.js file is essentially acting as a .css file) when angular.js is loaded (which is recommended to be in the head of your page). When angular actually runs on DOM load, it removes the ng-cloak attribute, uncloaking the elements.
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}
If you don't load angular in the head of your document, then you can add the styles manually to the head and everything will still work. Just add
<style type="text/css">
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}
</style>
This is a guess due to the fact that you only provided a small code snippet but my guess is that the controller is not initialized yet ( under the hood angular will parse through your HTML DOM and act accordingly when it is called. )
If you use a simple route this would prevent this completely.
Alternatively you could place all of your ng-if conditions with a css display:none; and then iterate through all of them once the app has completed loading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With