简体   繁体   English

在AngularJS中将json添加到作用域

[英]adding json to scope in AngularJS

I am concatenating two objects in json and assigning it to $scope in angularjs. 我在json中串联两个对象,并将其分配给angularjs中的$ scope。 But unable to do the same. 但是无法做到这一点。 The code is given below 代码如下

//https://wind-bow.hyperdev.space/twitch-api/channels/ESL_SC2

/*Global Variable*/
var responseData=[];
var responseDataValues=[];
var twitch = angular.module("twitch",[]);

twitch.controller("listStremers",streamMe);

function streamMe($scope,$http)
{
  var streamers=["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas","comster404","brunofin"];

  for(var i=0;i<streamers.length;i++)
    {
      $http({
        method:"jsonp",
        url:"https://wind-bow.hyperdev.space/twitch-api/streams/"+streamers[i],
        params:{ 
            format: 'json',
            callback: 'fitData'
        }
      });
      $http({
        method:"jsonp",
        url:"https://wind-bow.hyperdev.space/twitch-api/channels/"+streamers[i],
        params:{ 
            format: 'json',
            callback: 'fitDataValues'
        }
      });
    }
 $scope.twitchData=makeJson(responseDataValues,responseData);
  $scope.filStatus={};
  $scope.clear=function(){
    $scope.filStatus={};
    console.log($scope);
  }
}

function fitData(response)
  {

    responseData.push(response);
  }
function fitDataValues(response)
{
  console.log(response);
  responseDataValues.push(response);
}

function makeJson(arr1,arr2)
{

  for(var i=0;i<arr1.length;i++)
    {

      arr1[i].stream=arr2[2].stream;
      if(arr1[i].stream==null&&arr1[i].status==404)
      {
        arr1[i].liveStatus="offline";
      }
      else if(arr1[i].status==404)
      {
        arr1[i].liveStatus="dead";
      }
      else
      {
        arr1[i].liveStatus="online";
      }
      //console.log(arr1[i]);
      //console.log(arr2[i]);
    }
  //console.log(arr1);
  return arr1;
}

in the above code in makeJson function I have to create a new liveStatus,stream property in arr1[i] object and assign value to it but code get execute without any problem however no value get assigned to arr1 and same get assigned to $scope in controller. 在上面的makeJson函数中的代码中,我必须在arr1 [i]对象中创建一个新的liveStatus,stream属性,并为其分配值,但是代码可以毫无问题地执行,但是没有值被分配给arr1,相同的值被分配给$ scope控制器。 Am new to AJ and javascript, please help 是AJ和javascript的新手,请帮助

This problem is because the data you are concatenating, is coming from http call, which is asynchronous,before your data came through http call, the line 这个问题是因为您要串联的数据来自异步的http调用,在您的数据通过http调用之前,该行

 $scope.twitchData=makeJson(responseDataValues,responseData);

got executed with both blank array. 被两个空白数组执行。 You can do it either by promise resole, or with a simple way make following changes in your code : 您可以通过答应resole,也可以通过简单的方法在代码中进行以下更改来实现:

function fitData(response)
{
    responseData.push(response);
    if(responseDataValues.length){
        $scope.twitchData=makeJson(responseDataValues,responseData);
    }
}
function fitDataValues(response)
{
   console.log(response);
   responseDataValues.push(response);
    if(responseData.length){
        $scope.twitchData=makeJson(responseDataValues,responseData);
    }
}

the http call take the more time will invoke the function call with both array having values. http调用花费的时间更多,这两个数组都具有值的函数将调用函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM