0

I have declared an empty list in a controller that need to be updated by the callback of a method from a service:

.controller('appCtrl', function($scope, appService) {
  var list = [];
   appService.sMethod().then(function(result){
     list = result;
   });
    console.log(list);
  })

the method from the service is asynchronous:

.service('appService', function($timeout){
  return {
    sMethod: function(ctrlVar){
      return $timeout(function(){
        return ['response'];
      })
    }
  }
})

You can try it here: https://codepen.io/neptune01/pen/rKoLBj

Apparently it doesn't work. I know, there is a scoping issue. Tried with .bind(this) and assigning self = this but cannot get it work. At first I thought it is because it's updated asynchronously but looking at browsers console it doesn't show the updated object even after the callback is executed.

Any solution to this?

neptune
  • 1,211
  • 2
  • 19
  • 32

0 Answers0