Angularjs 为什么 for/loop 覆盖数组项
Angularjs why for/loop overriding the array items
我正在尝试使用twitch api,我需要知道哪个用户在线
//Initialize the App
var channels = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
var app = angular.module('Twitch', []);
//main controller
app.controller("MainController", function ($scope, $http) {
//Where controller variables are assigned
// create the game Object
var result = {};
$scope.results = [];
var stream = {};
var care = "";
$scope.streams = [];
var url = 'https://api.twitch.tv/kraken/channels/';
for (var x = 0; x < channels.length; x++) {
$http.jsonp(url + channels[x] + '?callback=JSON_CALLBACK').success(function (data) {
result = data;
var id = data._id;
var name = data.name;
$scope.url = "http://static-cdn.jtvnw.net/jtv_user_pictures/" + name + "-profile_image-" + id + "-300x300.jpeg";
$scope.image = data.logo;
$scope.name = data.name;
$scope.status = data.status;
$http.jsonp("https://api.twitch.tv/kraken/streams/" + channels[x] + "?callback=JSON_CALLBACK").success(function (data) {
var streaming = (data.stream === null) ? false : true;
if (streaming) {
result.off = 'online';
var streamTitle = data.stream.channel.status;
if (streamTitle.length > 36) {
streamTitle = streamTitle.substring(0, 33);
streamTitle += '...';
}
result.streamTitle = streamTitle;
} else {
result.off = 'offline';
data.streamTitle = '';
}
result.username = channels[x];
});
$scope.results.push(result);
console.log(result);
}).error(function (data) {
// something went wrong :(
console.log('Got nothing');
});
}
});
我创建了result.off
保存用户状态值的变量
(在线/离线)我在表格中显示此值
<div class="container text-center text-middle" ng-controller='MainController'>
<table class="table" ng-repeat="result in results">
<tbody>
<tr ng-class="satus">
<td><a class="media-left" href="#">
<img ng-src={{result.logo}} alt="image" class="img-circle img-middle " width='50px'> </a></td>
<td>
<h4 class="media-heading">{{result.name}} <span class="label"><p>{{result.off}}</p></span></h4>
</td>
<td><h4>{{result.status}}</h4></td>
</tr>
</tbody>
</table>
</div>
表达式 result.off 仅显示列表中的最后一个用户状态
尝试在
for - 循环中移动result
变量
当你做result = data;
你只是覆盖值并推送它。
生成的数组是同一对象的列表
变量结果空对象应在循环启动后实现
所以它可以在循环内迭代,否则值将被覆盖
channels.forEach(function(channel) {
$http.jsonp(url + channel + '?callback=JSON_CALLBACK').success(function(data) {
var result = {};
相关文章:
- 为什么用javascript覆盖2D数组的元素
- 如何在mongodb(mongose)中覆盖子文档的数组属性
- Javascript数组项被覆盖
- 无限多维数组,其中一个值被覆盖
- 如何覆盖 javascript 对象数组中的元素
- 阻止数组被覆盖或多次调用
- jquery插件选项数组,防止数组覆盖
- 按数组覆盖相似的条目
- 我的数组中的每个元素都被最后一个元素推入覆盖
- 在javascript中,如何将相同的对象两次推送到一个数组中,并进行一些修改而不覆盖数组
- Javascript:被推入数组的对象相互覆盖
- 覆盖原型继承中的数组
- 谷歌地图V3,如何获得标记/覆盖数组
- javascript每个覆盖数组对象
- 通过与另一个数组javascript匹配来覆盖数组元素值
- 覆盖数组中的重复值- Javascript
- 异步映射覆盖数组对象
- 将全局数组传递给函数将覆盖数组
- Array() 中的 getElementById() 正在覆盖数组中的其他元素
- Angularjs 为什么 for/loop 覆盖数组项