Use Promise and service together in Angular

后端 未结 2 722
醉话见心
醉话见心 2021-02-05 07:10

My question is based on this topic in Angular Google group.
I want to provide a service which stores some basic data retrieved from the backend via $http, then I only need t

2条回答
  •  清酒与你
    2021-02-05 07:53

    It's a bit of an overhead to create your own promise, angular's $http creates one for you anyway. What you're looking for is caching and http can handle it for you by passing cache:true to the service call.

    So you can simply do something like this:

     module.factory("userProvider", function() {
       var getUser = function() {
       return $http.get(..., {cache:true}).then(function(data) {
           return data.user; 
       });
    
       return {
           getUser : getUser
       }
    });
    

提交回复
热议问题