Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  1. What is the ng-cloak directive?
  2. Why do we use it?
like image 302
Anil Singh Avatar asked Feb 14 '15 10:02

Anil Singh


People also ask

What is the use of NG cloak in AngularJS?

Definition and Usage 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.

What is the use of ng init directive?

The ng-init directive is used to initialize an AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application.

What is the use of NG hide?

Definition and Usage The ng-hide directive hides the HTML element if the expression evaluates to true. ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none .

How do you use NG-bind?

Definition and UsageThe ng-bind directive tells AngularJS to replace the content of an HTML element with the value of a given variable, or expression. If the value of the given variable, or expression, changes, the content of the specified HTML element will be changed as well.


2 Answers

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                 });              });         });  }]);     
like image 94
Ben Diamant Avatar answered Sep 18 '22 16:09

Ben Diamant


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.

like image 35
user2935435 Avatar answered Sep 21 '22 16:09

user2935435