Googleapi - gapi 未按 angularjs 中的预期更新 $scope 对象

Googleapi - gapi not updating $scope object as expected in angularjs

我似乎从 gapi.client.drive.files.list 那里得到奇怪的行为,我无法根据承诺更新范围 returns。

angular.module('myApp').controller("folder_controller", function($scope, $q, $http, $rootScope) {
    $scope.list_subfolders = function () {
        gapi.client.drive.files.list({
            'q': "mimeType = 'application/vnd.google-apps.folder'",
            'fields': "nextPageToken, files(id, name, parents, mimeType, description, starred, properties)"
        }).then(function(response) {
            $scope.subfolders = response.result.files;
            console.log($scope.subfolders); 
        }, function(error){
            console.log(error);
        });
    }
});

当我 运行 list_subfolders()... console.log 显示 $scope.subfolders 很好...但是视图永远不会用该值更新 - 它是空的。我尝试了各种方法,包括只分配给 $rootScope,但我无法使用 $scope.subfolders.

更新视图

我是不是处理错了?我不明白为什么变量没有更新。

试试这个:

angular.module('myApp').controller("folder_controller", function($scope, $q, $http, $rootScope) {
$scope.list_subfolders = function () {
    gapi.client.drive.files.list({
        'q': "mimeType = 'application/vnd.google-apps.folder'",
        'fields': "nextPageToken, files(id, name, parents, mimeType, description, starred, properties)"
    }).then(function(response) {
        $scope.subfolders = response.result.files;

        // you need to refresh the view:
        $scope.$apply();

        console.log($scope.subfolders); 
    }, function(error){
        console.log(error);
    });
}

});