Angularjs 捕获$resource.ready事件
我使用angularjs中的$resource服务从服务器异步加载json内容。加载内容时,我会在页面上显示一个gif加载程序。我需要知道ansyncronous请求何时完成,以便删除加载程序。如何捕获就绪事件以便删除加载程序Angularjs 捕获$resource.ready事件,angularjs,Angularjs,我使用angularjs中的$resource服务从服务器异步加载json内容。加载内容时,我会在页面上显示一个gif加载程序。我需要知道ansyncronous请求何时完成,以便删除加载程序。如何捕获就绪事件以便删除加载程序 function PlaylistController($scope, $route, $http, Song,Artists,Albums,Drive,Children){ function render(){ $scope.songs = So
function PlaylistController($scope, $route, $http, Song,Artists,Albums,Drive,Children){
function render(){
$scope.songs = Song.list()
//Obviously this removes the loading class immediately
//I want to remove it when Song.list() is complete
$('body').removeClass('loading');
}
$scope.$on("$routeChangeSuccess", function($currentRoute, $previousRoute){
//When the route changes,update the $scope variables
render();
});
您不应该在控制器内部进行DOM操作 那么
$scope.load=true
和
呢
当您将$scope.loading
设置为true时,loading
类将添加到body中,当设置为false时,它将被删除
如果需要从子控制器更改它,请将其放入对象中(如
$scope.data.load=true
),或创建函数以更改它,如$scope.startLoading=function(){$scope.load=true;}
+1角度控制器应尽可能不操纵DOM。但是,一旦加载了内容,如何更改$scope.loading=false呢。加载开始时,您的方法会将加载类添加到主体中,但加载完成后,它不会将其删除。在将定义$scope.loading
的控制器中,只需调用=false
或创建类似$scope.stopLoading=function(){$scope.loading=false}
的函数即可供子作用域使用。ngClass对每个$digest表达式求值。在加载为false的摘要中,评估不会返回“加载”类,ngClass会理解需要删除它。看这个例子