Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html element inside ng-if appear when the page is loading

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>
like image 839
Hmahwish Avatar asked Feb 09 '15 05:02

Hmahwish


People also ask

What is * ngIf and how does it work?

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.

What is the difference between * ngIf and ngIf?

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">

Can we use data Ng instead of Ng If we want to make your page HTML valid?

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.

Does ngIf remove element from DOM?

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.


2 Answers

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>
like image 112
Joe Enzminger Avatar answered Sep 25 '22 10:09

Joe Enzminger


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

like image 26
Damian Nikodem Avatar answered Sep 24 '22 10:09

Damian Nikodem