使用google工作表API时Angular 1.6 $ http.jsonp

Yoa*_*adi 6 javascript jsonp angularjs angular-promise google-sheets-api

Angular 1.6的$ http.jsonp与google sheet的API不太搭配:

我正在尝试从谷歌工作表中获取并获取我的数据,具体如下:

var callback;
app.controller("meetingsTable", function ($scope, $http, $sce) {

var url = "http://spreadsheets.google.com/a/google.com/tq";
var trustedUrl = $sce.trustAsResourceUrl(url);
var key = 'MY_KEY';
var tq = 'select%20*%20limit%2010';
var tqx = 'responseHandler:callback';
var params = {
    key: key,
    tq: tq,
    status: 'ok',
    tqx: tqx
};

callback = function (response) {
    console.log(response); // entering here, not to the promise
    return response;
}


    $http.jsonp(trustedUrl, { params: params }).then(function (response) {
        console.log(response);
        retrun;
        //success things go here
    }, function (response) {
        //error things go here
    });
});
Run Code Online (Sandbox Code Playgroud)

我成功地通过使用函数(回调)和vnila js从表单中获取数据,当我尝试使用angular时,我在源代码中得到了一个"google.visualization.Query.setResponse"对象,控制台错误:未捕获的ReferenceError:未定义谷歌.

最讨厌的事情 - 承诺没有回复响应,我无法更新我的表的值ansyc.我尝试了所有我能想到的(以及stackoverflow中的每个建议),我尝试过的事情:

  1. 传递url,没有params,cuase myabe $ sce.trustAsResourceUrl需要整个url.
  2. 没有$ sce的传递(在vanila js工作,不在这里).
  3. 将我的承诺成功函数命名为"回调".
  4. 检查工作表API中的所有值是否在这里(再次使用vanila).
  5. 在promise中调用"callback",将其作为promise中的函数输入.
  6. 将所有jsonp放在一个返回响应的函数中,使用和不使用回调函数.
  7. 从"tqx = responseHandler:callback"参数中删除回调所有togther.
  8. 将承诺作为tqx参数中的回调传递.
  9. 使用1.5 <"JSON_CALLBACK",它不适用于1.6.
  10. 使用vanila js发出请求,然后将其传递给控制器​​(不能正常工作).

如果我记得更多的东西,我会在下面更新.

拜托,任何人都可以找出问题所在吗?非常感谢,谢谢,Yoav.

Yoa*_*adi 2

回答我自己的问题:

如果你们有同样的问题,请使用 Angular 的$scope.$apply属性。这是 Angular 的 API 中没有详细记录的属性,因此这里有一个很好的指南, 说明如何使用 $apply,并提供了一个很好的示例。我的实现:

$scope.tableContentData;
callback = function (response) {
    $scope.$apply(function () {
        $scope.tableContentData = response;
    });
};
$http.jsonp(trustedUrl).then(function () {
        //success stuff
    }, function () {
        //error stuff
    });
Run Code Online (Sandbox Code Playgroud)

当我在控制器外部声明回调时。

这是一场噩梦。

无论如何,感谢您的投票!