javascript - adding json to scope in AngularJS -
i concatenating 2 objects in json , assigning $scope in angularjs. unable same. code 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 above code in makejson function have create new livestatus,stream property in arr1[i] object , assign value code execute without problem no value assigned arr1 , same assigned $scope in controller. new aj , javascript, please help
this problem because data concatenating, coming http call, asynchronous,before data came through http call, line
$scope.twitchdata=makejson(responsedatavalues,responsedata);
got executed both blank array. can either promise resole, or simple way make following changes in code :
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 more time invoke function call both array having values.
Comments
Post a Comment