How to do preloader for `ng-view` in Angular JS?

后端 未结 3 1110
攒了一身酷
攒了一身酷 2021-01-07 00:21

I use

on web page. When I click link in block
is loaded HTML template was set in routeProvider
3条回答
  •  清酒与你
    2021-01-07 00:29

    Download javascript and css files from PACE Loader. Playing around with pace loader using ng-views . Hope this helps someone trying to use PACE.JS with Angular. In this example I am using ng-router to navigate between views.

    app.js

    var animateApp = angular.module('route-change-loader', ['ngRoute']);
    
    var slowResolve = function(slowDataService){
        return slowDataService.getContacts();
      };
      slowResolve.$inject = ['slowDataService'];
    
    // ROUTING ===============================================
    // set our routing for this application
      // each route will pull in a different controller
      animateApp.config(function($routeProvider) {
    
        $routeProvider
    
        // home page
        .when('/route1', {
          templateUrl: 'route1.html',
          controller: 'slowCtrl',
          controllerAs:'ctrl',
          resolve: {
            contacts:slowResolve
          }
        })
        .otherwise({
          templateUrl:'default.html'
        });
      });
    
    
    var SlowCtrl = function(contacts) {
      this.contacts = contacts;
    };
      SlowCtrl.$inject = ['contacts'];
    
      angular.extend(SlowCtrl.prototype, {
        message:'Look Mom, No Lag!',
        contacts: []
      });
    
    animateApp.controller('slowCtrl', SlowCtrl);
    
    
      var SlowDataService = function($timeout){
          this.$timeout = $timeout;
        };
        SlowDataService.$inject = ['$timeout'];
    
        angular.extend(SlowDataService.prototype, {
          contacts:[{
            name:'Todd Moto',
            blog:'http://toddmotto.com/',
            twitter:'@toddmotto'
          },{
            name:'Jeremy Likness',
            blog:'http://csharperimage.jeremylikness.com/',
            twitter:'@jeremylikness'
          },{
            name:'John Papa',
            blog:'http://www.johnpapa.net/',
            twitter:'@John_Papa'
          },{
            name:'Josh Carroll',
            blog:'http://www.technofattie.com/',
            twitter:'@jwcarroll'
          }],
          getContacts:function(){
            var _this = this;
            return this.$timeout(function(){
              return angular.copy(_this.contacts);
            }, 1000);
          }
        });
    

    animateApp.service('slowDataService', SlowDataService);

    index.html

    
    
    
      
      Test Example
      
      
      
      
      
      
      
      
      
    
    
    
      
    
    
    
    

    default.html

    Click on the tabs to change routes

    route1.html

    {{ctrl.message}}

    Name Blog Twitter
    {{contact.name}} {{contact.blog}} {{contact.twitter}}

提交回复
热议问题