Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngShow load delay issue

Tags:

angularjs

I have a menu that slides when some button is cliked, but at beginning this menu is hidden, something like that:

<div ng-show="menuShow">
  my menu here...
</div>

The issue is when the page is loaded the menu is not hidden (probably because the ngShow directive wasn't loaded) and then they disappears (probably because the ngShow directive was loaded) and making a strange "blink effect" with the menu.

What is the best way to deal with this issue??

like image 324
Daniel Faria Avatar asked Jan 15 '14 19:01

Daniel Faria


People also ask

Why is my ngshow and nghide toggle toggle not working?

When using ngShow and / or ngHide to toggle between elements, it can happen that both the element to show and the element to hide are visible for a very short time. This usually happens when the ngAnimate module is included, but no actual animations are defined for ngShow / ngHide.

Why does nganimate not work with ngshow/nghide?

Definitely surprising behavior though. ngAnimate is greedy since it assumes that all keyframes/transition code that is active on the element during any of the triggers are there to be animated via ngAnimate. Therefore, in this case, despite .loader having nothing to do with ngShow / ngHide, it is still picked up as a valid animation.

Why does my angular animation have a delay?

Those css transitions appear to be the real culprit, as angular-animate is picking up the durations from them. That is what causes the visual delay when showing/hiding those elements, even if you are not intentionally trying to animate that transition.

How to show or hide an HTML element using ngshow directive?

The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute. The element is shown or hidden by removing or adding the .ng-hide CSS class onto the element.


2 Answers

I couldn't get ng-cloak to work, even when applying the CSS rules, so I ended up using ng-if. https://docs.angularjs.org/api/ng/directive/ngIf

It results in a bit more overhead as Angular will actually remove the object from the DOM and reload it every time the value of ng-if changes, but in my case this was not very frequent and the overhead was not noticeable.

like image 42
SM3RKY Avatar answered Nov 12 '22 20:11

SM3RKY


The quickest and simplest thing to do would be to add the ngCloak directive to the element.

<div ng-show="menuShow" ng-cloak>
  my menu here...
</div>

As long as Angular is loaded synchronously in the head section of the document, this should prevent the flicker.

If you're not loading Angular synchronously, then according to the docs, you could manually add the CSS to the page:

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

And if this isn't possible for some reason, you can you just have all the Angular content not in the page on initial load, but included by an ng-include, ng-view or ui-view directive, or custom directive that includes its own template. (This is how I do it, but more for structural reasons)

like image 169
Michal Charemza Avatar answered Nov 12 '22 19:11

Michal Charemza