What is the best way to implement a loader animation while scripts and resources are loaded in Angular?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 10:27:07

问题


I want users of my web application to be presented with an progress bar indicating current progress of loading files and scripts necessary.

Preferably showing percentage, but a simple loader gif is an acceptable solution.

What is the best way to implement a loader that could show an indication of percentage finished loading images, css and js in Angular, if at all possible?

I'm thinking of having a small application whose only purpose is to load resources and indicate progress, and when all scripts and resources are finished, fire of some callback to boot angular (angular.bootstrap()).

My guess is that there are a bunch of smart guys that could come up with a better solution, and I hope and think that more than me can benefit from some good advice regarding this.

It would also be nice to be able to specify an estimated file size for loader correctedness:

<script load-src="//somehost.com/file.js" filesize="100"/>
<img load-src=sprite.png" filesize="50"/>

回答1:


Are you talking about just the loading of <script> tags and <link> tags etc? If so, you could use ng-cloak. Make everything hidden with ng-cloak CSS, and make a spinner shown by default. Then remove the spinner once angular fully loads. more info about ng-cloak

If you mean loading spinner until a set of your own HTTP requests are done, you could use angular-promise-tracker to track the HTTP requests and catch the 'start' and 'done' events to up the percentage.

Here's an example of the http requests loading spinner: http://plnkr.co/edit/SHtk7eSrbs1dfoQawPCW?p=preview




回答2:


For a bare bones, simple loader gif:

HTML:

<div ng-show="displayLoadingIndicator" class="LoadingIndicator">
</div>

CSS:

.LoadingIndicator {
  display: block;
  background: url(../img/loading.gif) center center no-repeat;
  position:absolute;
  top:0;
  left:0;
  width: 100%;
  height: 100%;
}

Controller JS:

function MyCtrl($scope, $http) {
    $scope.displayLoadingIndicator = true;
    $scope.loadStuff = function (serverResponse) {
        // load my stuff
        $scope.displayLoadingIndicator = false;


来源:https://stackoverflow.com/questions/14947295/what-is-the-best-way-to-implement-a-loader-animation-while-scripts-and-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!