谷歌API-GAPI未按AngularJS中的预期更新$ scope对象

时间:2019-02-21 14:09:10

标签: angularjs gapi

我似乎从gapi.client.drive.files.list中得到奇怪的行为,在这里我无法根据它返回的承诺更新作用域。

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更新视图。

我要解决这个错误吗?我不明白为什么变量没有更新。

1 个答案:

答案 0 :(得分:1)

尝试一下:

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);
    });
}

});