나는 트 위치 애플 리케이션에 최선을 다하고 있습니다. $ http를 사용하여 쉽게 할 수 있었지만 ngresource로 시도하기로 결정했습니다. 내가 겪고있는 문제는 오프라인 탭입니다. 사용자는 로고나 사용자 이름이 아니라 표시하고 있습니다. 스트림 약속이 오프라인 인 사용자에게 null을 반환하므로 로고/사용자 이름을 검색 할 수 없기 때문입니다. 어쨌든 $ scope.all 배열을 필터링하고 오프라인 사용자를 오프라인 탭에 배치 할 수 있습니까? 어떤 도움이라도 대단히 감사하겠습니다!각도 ngresource 및 다른 약속
http://codepen.io/labanch/pen/OXdKmK?editors=1011
.controller('TwitchController', ['$scope','$q', 'TwitchAPIChannel', function($scope, $q, TwitchAPIChannel) {
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
//var offlineUsers = [];
// Create promise for each channel/stream and store both in an empty object
var channelPromises= users.reduce(function(map, user) {
map[user] = TwitchAPIChannel.Channels.get({channel: user}).$promise;
return map;
}, Object.create(null));
var streamPromises = users.reduce(function(map, user) {
map[user] = TwitchAPIChannel.Streams.get({channel: user}).$promise;
return map;
}, Object.create(null));
// Calling promises for each channel/stream
$q.all(channelPromises).then(function(channels) {
$scope.allUsers = [];
//pushing all channels to the allUsers array
angular.forEach(channels, function(channel) {
$scope.allUsers.push(channel);
});
});
$q.all(streamPromises).then(function(streams) {
$scope.onlineUsers = [];
var offlineUsers = [];
$scope.offlineUsers = [];
angular.forEach(streams, function(stream) {
if(stream.stream){
$scope.onlineUsers.push(stream);
} else {
$scope.offlineUsers.push(stream);
}
});
});
//tabs
this.tab = 1;
this.setTab = function (tabId) {
this.tab = tabId;
};
this.isSet = function (tabId) {
return this.tab === tabId;
};
}])
// Twitch API Factory using $resource
.factory('TwitchAPIChannel', function TwitchAPIFactory($resource){
return {
Channels: $resource('https://api.twitch.tv/kraken/channels/:channel', {}, {
get: {
method: 'GET',
headers: {
Accept: 'application/vnd.twitchtv.v3+json',
'Client-ID': 'haibznywychj91wl2j76x1v1mx1rgwf'
}
}
}),
Streams: $resource('https://api.twitch.tv/kraken/streams/:channel', {}, {
get: {
method: 'GET',
headers: {
Accept: 'application/vnd.twitchtv.v3+json',
'Client-ID': 'haibznywychj91wl2j76x1v1mx1rgwf'
}
}
})
};
})
,이 질문의 코드와는 아무 상관이있다. – Phil
@Phil 나는 문제가있는 코드를 더 많이 언급하고 추가했습니다. – labanch