javascript - Angular nested GETs -
i have interesting situation getting list of ids 1 request , want grab first 1 , request image url object. unfortunately there times when id returned first request not valid , need check next id. attempting recursive callback way of doing not doing want , i'm getting no results due angular's asynchronous nature.
here sample of code:
app.controller('gr_menu', function($scope, grapi, flashmanager, catalogapi) { $scope.reports = []; grapi.reportlist().then( function(success) { success.data.foreach(function(report) { if(report.live) { $scope.reports.concat({'id': report.id, 'title': report.game, 'imgurl': getreportdata(report)}); } }); }, function(error) { flashmanager.flashmessage('danger', 'failed load game reports: '+error); }); function getreportdata(report) { catalogapi.searchgame(report.game, [{"json.offertype":"base game"}]) .then(function(success){ var = 0; return checkofferid(success.data.result[i].offerid, function (result) { i++; console.log(result) if (result != false) { return checkofferid(success.data.result[i].offertype); } else { return result; } }); }); } function checkofferid(offerid) { origin.games.cataloginfo(offerid) .then(function (catdata) { return catdata.customattributes.imageserver + catdata.localizableattributes.packartlarge; }, function (caterr) { return false; }); } });
so it's 3 requests deep. first list of our "reports" , there ids of games indicated in reports, , there images.
i getting nothing console.log perhaps i'm misunderstanding how callback should work. still trying familiarize myself working angular , callbacks. have implemented via simple loop , break if python.
i couldn't make sense of code (i'm getting first steps angularjs) think want following "app.js" (simple example using module controller , service):
angular.module('testapp', []) .controller('testctrl', ['$log','testservice', function($log, testservice) { var self = this; testservice.getuserlist().then(function (ld) { testservice.getuserinfo().then(function(ud){ //$log.log(ud.data.args.url); self.photo_url = ud.data.args.url; }) }); }]) .factory('testservice', ['$log', '$http', function($log, $http) { return { getuserlist: function() { return $http.get('https://httpbin.org/get?list=[{id:1,%20name:%20%22jhon%22},{id:2,%20name:%22lewis%22}]') }, getuserinfo: function() { return $http.get('https://httpbin.org/get?id=1&name=jhon&url=https://www.gravatar.com/avatar/346173670fc42829e26d7965cbc5c3e0') } }; }]);
then can bind "photo_url" value html, (index.html):
<html ng-app="testapp"> <body ng-controller="testctrl testctrl"> <img ng-src='{{testctrl.photo_url}}' alt="something kwel"/> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.js"> </script> <script src="app.js"></script> </body> </html>
hope have understood wanted, , hope helps well.
note: tested mocked urls http bin.
Comments
Post a Comment