javascript - AngularJS service to variable -


is there way store data variable?

i tried:

$scope.another =         function(){             var new_data;             userservice.getinfo().success(function(data){                new_data = data;             });             return new_data;         };  var data = $scope.another(); 

but returns 'undefined' in console log. thank you

edit

i empty array new_data .

var new_data = [];  $scope.another =                  function(callback){                     userservice.getinfo().success(function(data){                         paymentservice.getcashierparams({ "cardnumber": data.cardnumber}).success(function(data){                             gameservice.getallgames({ "pid":data.getcashierparameters.pid, "limit": 6, "skinid": 1}).success(function(data) {                                 callback(data.data.getflashgamesresult.data.flashgame);                             });                         });                     });                 };           $scope.another(function(result){                     new_data = result;                 });                 console.log(new_data); 

you need think problem differently. getinfo method returns promise. promise's success callback never called. may called sometime in future, in meantime code continue executing , return value of $scope.another undefined.

instead, place whatever logic wish execute within success callback.

userservice.getinfo().success(function (data) {     // stuff data. }); 

if not used working asynchronous data, may seem weird. better alternative, hanging page potentially many seconds while network request, database read, etc, completes.

if worried indentation, can create separate function(s) handle data.

function processdata(data) {     // process stuff...     return data; }  function handledata(data) {     data = processdata(data);     console.log(data); }  userservice.getinfo().success(handledata); 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -