Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - Hide content until DOM loaded

Tags:

angularjs

I am having an issue in Angularjs where there is a flicker in my HTML before my data comes back from the server.

Here is a video demonstrating the issue: http://youtu.be/husTG3dMFOM - notice the #| and the gray area to the right.

I have tried ngCloak with no success (although ngCloak does prevent the brackets from appearing as promised) and am wondering the best way to hide content until the HTML has been populated by Angular.

I got it to work with this code in my controller:

var caseCtrl = function($scope, $http, $routeParams) {     $('#caseWrap').hide(); // hides when triggered using jQuery     var id = $routeParams.caseId;      $http({method: 'GET', url: '/v1/cases/' + id}).         success(function(data, status, headers, config) {                                $scope.caseData = data;              $('#caseWrap').show(); // shows using jQuery after server returns data         }).         error(function(data, status, headers, config) {             console.log('getCase Error', arguments);         }); } 

...but I have heard time and time again not to manipulate the DOM from a controller. My question is how can I achieve this using a directive? In other words, how can I hide the element that a directive is attached to until all content is loaded from the server?

like image 754
Scotty Bollinger Avatar asked Aug 08 '13 18:08

Scotty Bollinger


People also ask

How to show hide in AngularJS?

The element is shown or hidden by removing or adding the . ng-hide CSS class onto the element. The . ng-hide CSS class is predefined in AngularJS and sets the display style to none (using an !

How to hide an HTML element via a button click in AngularJS?

First, wrap the content inside a <div> tag in an Html file. Now take a variable “toDisplay” and bind it to the <div> tag. In an HTML file, include a button and attach a function call to it whenever the user clicks on it.

What does ng cloak do?

The ng-cloak directive prevents the document from showing unfinished AngularJS code while AngularJS is being loaded. AngularJS applications can make HTML documents flicker when the application is being loaded, showing the AngularJS code for a second, before all code are executed.


2 Answers

In your CSS add:

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

and just add a "ng-cloak" attribute to your div like here:

<div id="template1" ng-cloak>{{scoped_var}}<div> 

doc: https://docs.angularjs.org/api/ng/directive/ngCloak

like image 108
Sebastien Horin Avatar answered Oct 03 '22 02:10

Sebastien Horin


On your caseWrap element, put ng-show="contentLoaded" and then where you currently have $('#caseWrap').show(); put $scope.contentLoaded = true;

If the caseWrap element is outside this controller, you can do the same kind of thing using either $rootScope or events.

like image 24
dnc253 Avatar answered Oct 03 '22 03:10

dnc253