Angular:如何在ui-router中实现“加载”动画gif?

时间:2015-03-02 20:11:50

标签: angularjs angular-ui-router

我的AngularJS配置中有以下声明:

.state('state1', {
     templateUrl: 'some_template.html',
     controller: 'chartCtrl',
     resolve: {
        chart: function(load){
             return loadChart.load();
        }
     }
})

控制器chartCtrl仅在loadChart.load功能结束时启动,因此我无法在控制器中管理打开/关闭GIF动画的指示器。有任何想法吗?

1 个答案:

答案 0 :(得分:2)

在您的应用运行配置文件中尝试:

$rootScope.$on('$stateChangeStart', function(){
   loadChart.load();
});

$rootScope.$on('$stateChangeSuccess', function(){
   loadChart.end();
});

控制器启动任何预加载是个坏主意,因为您无法将其重新用于其他控制器,并且需要更加可控。

您可以仅为解析器状态定义它,例如:

$rootScope.$on('$stateChangeStart', function(event, toState){
   if(toState.resolve){
     loadChart.load();
   }
});

$rootScope.$on('$stateChangeSuccess', function(){
   loadChart.end();
});

详细了解State Changes Events