search

Home  >  Q&A  >  body text

angular.js - How to synchronize the value obtained from the angular service outside the service?

monitorService.getFMCMonitor({ scenario: 'indoor'},function(res){
     $scope.data = res;
 });
 //$scope.$watch('data',(n,o)=>console.log(n));
 console.log($scope.data); ==> undefined

monitorService is a service that obtains background data. If $watch is not used, how to synchronize the value of $timeout with $scope.data? Just print $scope.data, which is the value obtained from the service, not undefined

漂亮男人漂亮男人2830 days ago664

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-15 17:11:27

    Because it is asynchronous, the ajax request does not return when you print, so it is naturally undefined.

    You can do this, pass a callback as a parameter, or a promise, both are fine.

    reply
    0
  • Cancelreply