How to use the actual use of ng-Cloak directive in AngularJs?
Asked Answered
R

3

39
  1. What is the ng-cloak directive?
  2. Why do we use it?
Raucous answered 14/2, 2015 at 10:16 Comment(3)
Have you seen superman aka clark kent and his epic move where he unbuttons his shirt showing his suit partially in a dark alley. Now what if he crossed the whole alley while he has not yet undressed completely and transformed into a superhero with his shiny suit with an underwear over it, whoops. So cartoonist made sure that the alley was long enough to not happen that mishap. And yes here the alley is our beloved ngcloak, clark kent is html and superman obviously is angularjs. ;)Brazell
It might help code-sample.com/2015/02/ng-cloak-directive-in-angularjs.htmlRaucous
@AnilSingh Hi Anil, will you be able to help on this Kendo issue #61335424Niphablepsia
A
49

ng-cloak

From the docs:

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.

In brief words, you can use ng-cloak directive to prevent uncompiled elements from being displayed. Uncompiled element can be an element that hold and wait for incoming data:

<div ng-cloak>{{myvar}}</div>

if myvar controller still not compiled or the data is not populated ng-cloak prevent {{myvar}} from being displayed and will only display the div when the variable is compiled.

Follow this code example and check to results with and without ng-cloak:

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

<body ng-controller="MyController" ng-cloak>
    <h3>ngCloak Example</h3>
        <ol >
            <li ng-repeat="item in myData">
                {{item.title}}
            </li>
        </ol>
</body>


var myApp= angular.module("myApp",['ngResource']);

myApp.controller("MyController", ["$scope", "$resource","$timeout",
    function($scope,$resource,$timeout){
        $scope.myData =[];

        var youtubeVideoService = $resource("https://gdata.youtube.com/feeds/api/videos?q=googledevelopers&max-results=5&v=2&alt=jsonc&orderby=published");

        youtubeVideoService.get()
            .$promise.then(function(responseData) {
        
        angular.forEach(responseData.data.items,
            function(aSingleRow){
                console.log(aSingleRow);
                $scope.myData.push({
                    "title":aSingleRow.title
                }); 
            });
        }); 
}]);    
Adversative answered 14/2, 2015 at 10:22 Comment(1)
when would you NOT want to have ng-cloak? it seems there is no downsideMagda
D
12

The reason given for using ng-cloak Ben is valid, however the outcome of the example provided by Ben will in some situations still display the {{var}}. This is particularly true in the wild when scripts are generally loaded asynchronously or placed at the bottom of any html body.

In Ben's example he's put a <style> at the top but doesn't use it, we should placed the ng-cloak class on the <body>, like so, and use that styling:

<body class="ng-cloak" ng-controller="MyController" ng-cloak> ...

This way the content of the body tag will not be shown until Angular changes ng-cloak to display: block or the directive updates the tagged html.

The reason for adding the class is because the ng-cloak directive is parsed after the html has been displayed, so there is always the possibility that your JS thread dies and still displays anything like {{something here}}.

A good example of proper use would be to include the class="ng-cloak" and ng-cloak directive on an ng-repeat directive.

However, if its just the {{}} thats annoying and otherwise the page is fine even when JS thread has crashed, its better to use ng-bind on your tags rather than adding {{}} within them.

Damato answered 2/12, 2015 at 9:3 Comment(0)
I
2

One note I would like to add - I have seen for most of the application, just adding the ng-cloak doesn't work. Its because that page might be larger and js not being loaded till that time.

Applying manually CSS for this directive would help here -

[ng-cloak]  
{  
display: none !important;
}

Hope this would be help to someone!

Intendant answered 8/9, 2017 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.