AngularJS strategy to prevent flash-of-unstyled-content for a class

℡╲_俬逩灬. 提交于 2019-11-28 04:04:21

What you are looking for is ng-cloak.
You have to add it like this:

<body class="{{ bodyClass }}" ng-cloak>

and this will prevent unwanted flashing.
Link to docs about this.

Edit:
It is also advisable to put the snippet below into your CSS file, according to docs.

"For the best result, the angular.js script must be loaded in the head section of the html document; alternatively, the css rule above must be included in the external stylesheet of the application."

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

One of the problems that you're facing is that the browser will display the <body> element before AngularJS has loaded and started manipulating the DOM. What other people said about ng-class is correct, it will do the right class applying but you still need to not show anything before Angular is ready.

In previous versions of Angular you could do this:

<body style="display:none" ng-show="true" ng-class="{{bodyClass}}">

In recent versions this doesn't work however because ng-show does its visibility by adding and removing the ng-hide class (which is less specific than an element attribute)

What I've been doing recently is this:

<head>
...
    <style> <!-- Or you could include this in an existing style sheet -->
        .ng-cloak {
            display: none !important;
        }
    </style>    
</head>
<body class="ng-cloak" ng-cloak ng-class="{{bodyClass}}">

Doing it this way means that the <body> will be immediately hidden by the style for the ng-cloak class. When Angular does start up it will process all of the directives include ng-class and ng-cloak, so your <body> element will then have the right class and be visible.

Read more here ng-cloak directive

There is a solution to the flash problem in plain CSS which is bulletproof. For the class of your app root element add the following. This solution works by order of processing: everything is hidden in CSS, angular is loaded, and then CSS shows the app root. I like to use this so if there are other non-angular elements on the page they will render first making the page appear to load faster.

/* in your CSS file in head */
.myApp {
    display:none;
}

<div class="myApp" ng-app="myApp"></div>

Just before the body ends add your script references and some inline CSS:

<script language="javascript" type="text/javascript" src="angular.min.js"></script>
<style>
.myApp {
    display:block;
}
</style>
</body>

Instead of using class, you should use ng-class

<body ng-class="{{ bodyClass }}">

I've noticed sometimes even ng-cloak doesn't seem to actually work. Maybe I am doing something wrong.

<body ng-cloak class="{{ bodyClass }}">

I also do one more small thing similar to @mbokil you can hide the element by default. By using an attribute selector targeting any elements with the attribute ng-cloak you're able to utilize angular directives. This has several benefits, but the main benefit is once angular fires it removes this attribute which will trigger a redraw.

<style>
  [ng-cloak] {
    display:none;
  }
</style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!