angularjs - Googleapi - gapi 未按预期在 angularjs 中更新 $scope 对象
问题描述
我似乎从 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 更新视图。
我接近这个错误吗?我不明白为什么变量没有更新。
解决方案
尝试这个:
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);
});
}
});
推荐阅读
- sql - 在推文字符串中删除 @ImSRK 的大查询
- android - AAPT:错误:意外元素
在发现 - angular7 - ERROR TypeError: _co.Get_products is not a function
- macos - 管道后“找不到命令”
- excel - 如何隐藏多个工作表中的多个列
- c# - 带有标头和正文的 HTTPClient 发布请求
- python - 无法从“keras.layers”导入名称“Merge”
- bash - 使用 sed 按序列拆分文件。重击
- javascript - 如何检查对象数组中是否存在id值
- npm - 在生产中运行 npm run prod 时出错