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

北城余情 提交于 2019-12-05 21:51:05
Andrew Joslin

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

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