Angularjs为什么/ loop覆盖数组项

时间:2016-07-14 10:35:21

标签: javascript angularjs angularjs-directive angularjs-scope angular-ui-router

我正在尝试使用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仅显示列表中的最后一个用户状态

2 个答案:

答案 0 :(得分:0)

尝试在for - loop

中移动result变量

当你执行result = data;时,只需覆盖该值并推送它。

结果数组是同一对象的列表

答案 1 :(得分:0)

应该在循环开始后实现变量结果空对象

所以它可以在循环内迭代,否则值将被重写

channels.forEach(function(channel) {
   $http.jsonp(url + channel + '?callback=JSON_CALLBACK').success(function(data) {
       var result = {};