【发布时间】:2017-10-17 15:51:55
【问题描述】:
我试图用//Second JSONP 回调函数更新channel.status 对象。
angular.element(document).ready(function() {
channels = [];
channel = {};
followersAPI = followersAPI.replace(/theUser/, user);
followersAPI = $sce.trustAsResourceUrl(followersAPI);
// First JSONP
$http.jsonp(followersAPI).then(function (response) {
var follows = response.data.follows;
for (var i = 0; i < follows.length; i ++) {
var currentChannel = follows[i].channel;
if (currentChannel.status == null) {
currentChannel.status = "offline";
}
channel = {
name: currentChannel.name,
display_name: currentChannel.display_name,
logo: currentChannel.logo,
url: currentChannel.url,
status: "loading"
};
streamAPI = "https://wind-bow.glitch.me/twitch-api/streams/";
streamAPI += channel.name;
streamAPI = $sce.trustAsResourceUrl(streamAPI);
// Second JSONP
$http.jsonp(streamAPI).then(function (response) {
data = response.data;
if (data.stream == null) {
channel["status"] = "offline";
} else {
channel["status"] = data.stream.channel.status;
}
});
channels.push(channel);
}
});
$scope.channels = channels;
console.log($scope.channels);
});
没有错误消息,但仅更新了 channels[] 数组中的最后一个 channel{} 对象。
这是channel.status 的 HTML 部分:
<div id="channel-status" class="col-md-6">
<a
class="btn"
href="{{channel.url}}"
target="_blank">
{{channel.status}}
</a>
</div>
【问题讨论】:
标签: javascript angularjs jsonp