0

我試圖用抽搐API和我需要了解哪些用戶在線Angularjs爲什麼/循環重寫數組項

//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僅表達示出了在t時的最後一個用戶狀態他列出

+0

編輯:我創建的變量這是reult.off(未user.off),以保持對用戶 (ONLIN /離線)狀態值和即時表示喲內表 –

+0

這個值ur結果數組檢查result.off每個用戶的值是否包含數據 –

+0

只有最後一個具有off屬性的對象 –

回答

0

嘗試移動內部result變量的 - 環

當你做result = data;你只是簡單地覆蓋值,並將它推。

所得陣列是相同的對象的列表

0

可變結果空物體應該在循環之後被實現開始

,因此它可以在循環否則該值將被overritten

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