捕获$resource.ready事件



我正在使用angularjs中的$resource服务从服务器异步加载json内容。加载内容时,我在页面上显示一个 gif 加载器。我需要知道异步请求何时完成,以便我可以删除加载程序。如何捕获就绪事件以便删除加载程序?

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.loading = true<body ng-class="loading && 'loading'">呢?

当您将$scope.loading设置为 true 时,loading 类将被添加到 body 中,当设置为 false 时,它将被删除。

如果您需要从子控制器更改它,请将其放入对象(如$scope.data.loading = true)或创建函数来更改它,如$scope.startLoading = function(){ $scope.loading = true; }

最新更新