我是否应该创建一个.factory来共享一个变量';两个ajax请求的值是多少?
我使用angularjs发出了两个ajax json请求,第一个请求是检索频道的在线/离线状态,第二个ajax json请求是获取频道的信息(徽标、名称、状态等) 我将变量信号分配给'data.stream',这是通道在json请求之间共享信号的在线/离线状态。在googledeveloper控制台中,我收到一个null值。我在这里做了一些研究,发现使用服务可能是一个解决方案。我按照说明进行了操作,但仍然无法获得json请求与识别信号之间的范围 我读到可以使用rootscope,但不推荐使用,不确定这是否正确,因为初学者使用angular,但我希望通过应用最佳实践开始我的angular之旅 概述:使用Angular Ajax向twitch api发出jsonp请求,我将发出两个请求,一个用于检索拖缆阵列中通道的在线/离线状态,另一个Ajax json请求用于检索通道的信息,我需要在ajax jsonp请求之间看到已分配值data.stream的变量信号的范围。请让我知道这是一个合乎逻辑的方法,还是我要“再次环游世界” 这是一个plunker:plnkr.co/edit/6sb39NktX7CwfnQFxNNX我是否应该创建一个.factory来共享一个变量';两个ajax请求的值是多少?,ajax,angularjs,service,jsonp,Ajax,Angularjs,Service,Jsonp,我使用angularjs发出了两个ajax json请求,第一个请求是检索频道的在线/离线状态,第二个ajax json请求是获取频道的信息(徽标、名称、状态等) 我将变量信号分配给'data.stream',这是通道在json请求之间共享信号的在线/离线状态。在googledeveloper控制台中,我收到一个null值。我在这里做了一些研究,发现使用服务可能是一个解决方案。我按照说明进行了操作,但仍然无法获得json请求与识别信号之间的范围 我读到可以使用rootscope,但不推荐使用,不
// creating a service for signal ?
app.factory('signal', function() {
var signal = {};
return signal;
})
// declaring my TwitchController and dependencies
app.controller('TwitchController', ['$scope', '$http', 'signal', function($scope, $http, signal) {
// streamers array with array of channels
var streamers = ["freecodecamp", "storbeck", "terakilobyte", "habathcx", "RobotCaleb", "thomasballinger", "noobs2ninjas", "beohoff", "medrybw"];
$scope.imgs;
$scope.signal = signal;
var self = this;
//empty array
self.info = [];
for (var i = 0; i < streamers.length; i++) {
var url = "https://api.twitch.tv/kraken/channels/";
var streamUrl = "https://api.twitch.tv/kraken/streams/";
var callback = "/?callback=JSON_CALLBACK";
//json ajax request for channels online and offline status
$http.jsonp(streamUrl + streamers[i] + callback).success(function(data) {
//provides status of shows online and offline
signal = data.stream;
console.log(signal)
});
// json ajax request for channels and channel's info
$http.jsonp(url + streamers[i] + callback).success(function(data) {
// if channel does not have a logo image, this jpg will be the placeholder
// if statement test channel status (online or offline)
if (!signal) {
signal = "offline";
} else if (signal) {
signal = 'online';
}
// pushing retreive data from twitch.tv into array self.info
self.info.push(data);
});
//为信号创建服务?
应用程序工厂('信号',功能(){
var信号={};
返回信号;
})
//声明我的TwitchController和依赖项
app.controller('TwitchController',['$scope','$http','signal',函数($scope,$http,signal){
//带通道阵列的拖缆阵列
var拖缆=[“freecodecamp”、“storbeck”、“TB”、“habathcx”、“RobotCaleb”、“thomasballinger”、“Noobs2nijas”、“beohoff”、“medrybw”];
$scope.imgs;
$scope.signal=信号;
var self=这个;
//空数组
self.info=[];
对于(var i=0;i
您这里的问题是对$http的异步调用。您需要使用$http并链接承诺。此实例不需要工厂,但最好使用一个只返回您的信息对象的工厂。我不知道您想要的格式,所以创建了一个。以下是plunker:
medrybw全天候广播,因此状态应该是在线的。在chrome中,Placeholder logo.jpg代表所有频道的徽标,而不应该是,更重要的是,频道的信息不再填充我的表达式。我做错了什么?你应该在plunkr中发布代码,以便跟踪你的问题。不清楚是什么对象您要生成。您可以更改$scope.info.push()根据需要包含来自channelData对象的尽可能多的数据。不确定联机状态。我只知道它从data.stream返回空状态,这是您用来确定它是否脱机/联机的。代码起作用,您只是没有解释您真正想要的结果对象。Hi@enkode这里是我的Plunker的URL我更新了Plunker以解决您的问题您正在注入信号,但没有信号服务…它现在可以工作,您只需跟踪您的在线-离线逻辑,因为您说这是不正确的。此外,您还必须设置一个正确的缺失图像,因为没有提供。这现在正在工作,请标记答案正确。我想我发现您希望data.status确定它们是否在线/离线。我更新了此场景的plunkr。现在您应该将默认映像更改为实时托管映像。
var app = angular.module('Twitch', []);
// declaring my TwitchController and dependencies
app.controller('TwitchController', ['$scope', '$http', function($scope, $http) {
// streamers array with array of channels
var streamers = ['freecodecamp', 'storbeck','terakilobyte', 'habathcx', 'RobotCaleb', 'thomasballinger', 'noobs2ninjas', 'beohoff', 'medrybw' ];
//empty array
$scope.info = [];
var url = 'https://api.twitch.tv/kraken/channels/';
var streamUrl = 'https://api.twitch.tv/kraken/streams/';
var callback = '/?callback=JSON_CALLBACK';
angular.forEach(streamers, function(stream) {
//json ajax request for channels online and offline status
$http.jsonp(streamUrl + stream + callback).then(function (data) {
//provides status of shows online and offline
// json ajax request for channels and channel's info
$http.jsonp(url + stream + callback).then(function (channelData) {
// pushing retrieve data from twitch.tv into array self.info
$scope.info.push(
{
url: url + stream,
stream: stream,
status: !data.stream ? 'offline' : 'online', // ternary statement test channel status (online or offline)
logo: channelData.data.logo ? channelData.data.logo : 'placeholderlogo.jpg' // if channel does not have a logo image, this jpg will be the placeholder
}
);
});
});
});
}]);