Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent element show before AngularJS initialized( ng-show )

In AngularJS, I wonder how to prevent the elements shown on page before ng-show take effect, I found some posts talk about ng-cloak, but it seems not work in my case, probably the ng-cloak is for prevent double curly bracket rather than Element style.

Another way someone talk about is define some style for before AngularJS initialized, but that is kinda hard to manage.

Is there some official way to handle this?

like image 996
Kuan Avatar asked Jun 27 '15 00:06

Kuan


People also ask

What is the use of NG cloak?

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.

Where is the last element in NG repeat?

$first and $last It's common when using ng-repeat to add specific behavior to the first or last element of the loop, e.g. special styling around the edges. Instead, ng-repeat already supplies you with two ready boolean properties. $first is true for the first element, and $last is true for the last element.

What is Ngshow in angular?

The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements.

What is Ng click?

ng-click is an attribute of an HTML element that can be used for the customized components in Angular. We use this attribute to detect if an element is clicked and tracks the mouse's cursor. ng-click takes a function as an attribute and calls it whenever an element is clicked.


2 Answers

Unless you want to show a loader, ng-cloak should be your solution.

Official documentation on ng-cloak

If you still have the issue, you may try to add the css to hide element with ng-cloak inside your html to be sure the browser has it in time.

If you do that, choose on way to add the ng-cloak. For example add it as class:

<div ng-show="condition" class="ng-cloak">...</div>

And add this into your html head tag:

<style> .ng-cloak { display: none !important; } </style>
like image 182
Yoann Avatar answered Sep 21 '22 16:09

Yoann


In case you want to just avoid showing something till it's ready to be shown (some data has been loaded from the backend perhaps) then it's better to use ng-if. Ofcourse it works the same with ng-show. But the advantage of using ng-if is that you delay the creation of the extra DOM until it needs to be shown and as a result you improve the intial page loading time.

Here is an example:

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


   
myApp.controller("myController", function ($scope, $timeout) {

    $scope.isLoading = false;
    $scope.data = null;
    
    simAsync();

    //simulate async task like http request
    function simAsync() {
        //loadind data has started
        $scope.isLoading = true;

        $timeout(function () {
            $scope.data = [{
                "firstname": "Sims",
                    "lastname": "Wilkerson"
            }, {
                "firstname": "Kelli",
                    "lastname": "Vazquez"
            }, {
                "firstname": "Mcdonald",
                    "lastname": "Byrd"
            }, {
                "firstname": "Taylor",
                    "lastname": "Frost"
            }, {
                "firstname": "Merle",
                    "lastname": "Adkins"
            }, {
                "firstname": "Garrett",
                    "lastname": "Hood"
            }];
            //the data has loaded
            $scope.isLoading = false;
        }, 1500);
    }

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
    
</div>
like image 31
Christos Baziotis Avatar answered Sep 19 '22 16:09

Christos Baziotis