Angular 1.6 $http.jsonp 同时使用 google 张 API
Angular 1.6 $http.jsonp while using the google sheets API
Angular 1.6 的 $http.jsonp 与 google 床单的表现不佳 API:
我正在尝试从 google 表中获取然后获取我的数据,其中包含以下内容:
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
});
});
我成功地从工作表中获取数据,通过使用函数(回调)和 vnila js,当我尝试使用 angular 时,我得到了一个 "google.visualization.Query.setResponse" 对象来源,控制台错误:Uncaught ReferenceError: google is not defined.
最烦人的事 - promise 没有收到响应,而且我无法更新我的 table 值 ansyc。
我尝试了所有我能想到的(以及 Whosebug 中的每一个建议),
我尝试过的事情:
- 按原样传递 url,没有参数,因为 $sce.trustAsResourceUrl 需要整个 url.
- 在没有 $sce 的情况下通过(在 vanila js 中有效,不适用于此处)。
- 将我的承诺成功函数命名为 "callback"。
- 检查工作表 API 中的所有值是否都在这里(同样,使用 vanila)。
- 在 promise 中调用 "callback",将其作为函数输入到 promise 中。
- 获取一个函数内的所有 jsonp return 响应,有/没有回调函数。
- 一起从 "tqx=responseHandler:callback" 参数中删除回调。
- 在 tqx 参数中将承诺作为回调传递。
- 使用 1.5< "JSON_CALLBACK",它不适用于 1.6。
- 使用 vanila js 发出请求,然后将其传递给控制器(不起作用)。
如果我还记得更多的话,我会在下面更新。
请问,谁能找出问题所在?
非常感谢,
谢谢,
Yoav.
回答我自己的问题:
如果你们遇到同样的问题,请使用angular的$scope.$apply property. this is a not so well-documented property in Angular's API, so here's a nice guide
对于何时如何使用 $apply,有一个很好的例子。
我的实现:
$scope.tableContentData;
callback = function (response) {
$scope.$apply(function () {
$scope.tableContentData = response;
});
};
$http.jsonp(trustedUrl).then(function () {
//success stuff
}, function () {
//error stuff
});
当我在控制器外声明回调时。
这是一场噩梦。
无论如何感谢您的投票!
Angular 1.6 的 $http.jsonp 与 google 床单的表现不佳 API:
我正在尝试从 google 表中获取然后获取我的数据,其中包含以下内容:
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
});
});
我成功地从工作表中获取数据,通过使用函数(回调)和 vnila js,当我尝试使用 angular 时,我得到了一个 "google.visualization.Query.setResponse" 对象来源,控制台错误:Uncaught ReferenceError: google is not defined.
最烦人的事 - promise 没有收到响应,而且我无法更新我的 table 值 ansyc。 我尝试了所有我能想到的(以及 Whosebug 中的每一个建议), 我尝试过的事情:
- 按原样传递 url,没有参数,因为 $sce.trustAsResourceUrl 需要整个 url.
- 在没有 $sce 的情况下通过(在 vanila js 中有效,不适用于此处)。
- 将我的承诺成功函数命名为 "callback"。
- 检查工作表 API 中的所有值是否都在这里(同样,使用 vanila)。
- 在 promise 中调用 "callback",将其作为函数输入到 promise 中。
- 获取一个函数内的所有 jsonp return 响应,有/没有回调函数。
- 一起从 "tqx=responseHandler:callback" 参数中删除回调。
- 在 tqx 参数中将承诺作为回调传递。
- 使用 1.5< "JSON_CALLBACK",它不适用于 1.6。
- 使用 vanila js 发出请求,然后将其传递给控制器(不起作用)。
如果我还记得更多的话,我会在下面更新。
请问,谁能找出问题所在? 非常感谢, 谢谢, Yoav.
回答我自己的问题:
如果你们遇到同样的问题,请使用angular的$scope.$apply property. this is a not so well-documented property in Angular's API, so here's a nice guide 对于何时如何使用 $apply,有一个很好的例子。 我的实现:
$scope.tableContentData;
callback = function (response) {
$scope.$apply(function () {
$scope.tableContentData = response;
});
};
$http.jsonp(trustedUrl).then(function () {
//success stuff
}, function () {
//error stuff
});
当我在控制器外声明回调时。
这是一场噩梦。
无论如何感谢您的投票!