angularjs - how to use angular and JavaScript promises to return some data? - Stack Overflow

I am trying to grab some data from local storage, but I want to use the date once is ready. something l

I am trying to grab some data from local storage, but I want to use the date once is ready. something like:

// this belongs to a Helper factory
getData: function(){
        var deferred = jQuery.Deferred();
        setTimeout(function(){
            var data = {
                description: localStorageService.get('description'),
            };
            deferred.resolve()
        }, 10);
        return deferred.promise();
  },

// I set it up so that in the controller I can do
// $scope.data = Helper.getData();

Similar to the example i have, i just want to return the data object once it has the description from localstorage

I am using angularjs, so i want to display {{data}} in my view once the promise is resolved. like maybe use ng-hide or something similar.

In any case, my question is about setting up that promise.

Any ideas?

I am trying to grab some data from local storage, but I want to use the date once is ready. something like:

// this belongs to a Helper factory
getData: function(){
        var deferred = jQuery.Deferred();
        setTimeout(function(){
            var data = {
                description: localStorageService.get('description'),
            };
            deferred.resolve()
        }, 10);
        return deferred.promise();
  },

// I set it up so that in the controller I can do
// $scope.data = Helper.getData();

Similar to the example i have, i just want to return the data object once it has the description from localstorage

I am using angularjs, so i want to display {{data}} in my view once the promise is resolved. like maybe use ng-hide or something similar.

In any case, my question is about setting up that promise.

Any ideas?

Share Improve this question edited Aug 7, 2014 at 4:48 mehdi lotfi 11.6k18 gold badges83 silver badges128 bronze badges asked Aug 7, 2014 at 4:44 PatrioticcowPatrioticcow 27.1k76 gold badges221 silver badges340 bronze badges 2
  • Sidenote, if you're using angular, don't use jQuery.Deferred, (Actually, as a general rule, don't use jQuery.Deferred) $q is a much better promise library. – Retsam Commented Aug 7, 2014 at 4:51
  • @Retsam. I'm open to any good solution – Patrioticcow Commented Aug 7, 2014 at 4:52
Add a ment  | 

2 Answers 2

Reset to default 5

$timeout() returns a promise, so this works:

getData: function(){
   return $timeout(function(){
       return localStorageService.get('description'),
   },3000);
}

usage:

getData().then(function(data){
   console.log(data);
})

Well $q is the library normally used for handling promises in angularjs, since you're reading from local storage and that is a synchronous operation I'm not sure why you want to return a promise, the way this is usually done in angular however is like this:

getData: function(){
   var deferred = $q.defer();
   $timeout(function(){
     var data=localStorageService.get('description'),
     deferred.resolve(data);
   },3000);
   return deferred.promise;
}

usage:

getData().then(function(data){
   console.log(data);
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744787270a4593703.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信