您好我正在嘗試使用import.io刮一些足球比分。我設法讓他們的JS使用API並提供數據。問題在於它必須在控制器的私人範圍內,因爲我無法對其進行重複操作。從第三方庫異步回調更新AngularJS範圍
任何人都可以告訴我爲什麼,如果任何人有一個很好的指導範圍,可能會更有用。
latestScores.controller('ScoresController', function ($scope) {
$scope.pots = [];
var io2 = new importio("XXX", "XXXXXX[API KEY]XXXXXXXX", "import.io");
io2.connect(function (connected) {
if (!connected) {
console.error("Unable to connect");
return;
}
var data;
var callback = function (finished, message) {
if (message.type == "DISCONNECT") {
console.error("The query was cancelled as the client was disconnected");
}
if (message.type == "MESSAGE") {
if (message.data.hasOwnProperty("errorType")) {
console.error("Got an error!", message.data);
} else {
data = message.data.results;
}
}
if (finished) {
pots = data;
console.log(pots); /* This gives me an object */
}
}
io2.query({
"connectorGuids": [
"d5796d7e-186d-40a5-9603-95569ef6cbb9"],
}, callback);
});
console.log($scope.pots); /* This gives me nothing */
});
你asigning數據到本地變量盆,嘗試將其分配給$回調scope.pots – ecyshor
@NicuReut不是本地 - 花盆 - 在窗口中是全局變量:-) – Grundy