2014-04-11 30 views
1

我有一個$ http服務角js有一個緩存啓用它。第一次加載應用程序服務時獲取調用並獲取緩存。現在,當我從同一頁的任何地方調用服務時,數據來自緩存,但是當我更改頁面路由並再次從另一頁調用服務時,數據來自服務器(我只是更改路由而不刷新頁面)

編輯=> 代碼正在工作!在路由上,數據也來自緩存,但由於其他呼叫很少,因此需要更多時間。它只是把更多的時間的話,我接受了緩存響應。如果我同一調用任何click事件,然後將需要2ms的至3ms的

,這裏是我的服務從控制器

commonServicesModule.service('getDetails', ['$http', 'urlc', 
    function($http, urlc) { 

     return { 
      consumer: { 
       profile: function(id) { 
        return $http({ 
         url: urlc.details.consumer.profile, 
         cache: true, 
         params: { 
          'consumer_id': id, 
          'hello': id, 
         }, 
         method: 'GET', 
        }).success(function(result) { 
         return result; 
        }); 
       }, 

      } 
     } 
    } 
]) 

電話:

start = new Date().getTime(); 
      /*get user information */ 
      getDetails.consumer.profile('1').then(function(results) { 

       console.log('time taken for request form listCtrl ' + (new Date().getTime() - start) + 'ms'); 
      }); 

當我從路由後的任何其他地方調用它時需要相同的時間。

+0

嘗試從回拉你的「消費者」對象,將其向上移動到函數體,然後在您的回報中引用它,而不是在那裏定義它。 –

+0

@marck ..沒有得到你可以üPLZ把代碼? –

回答

0

嘗試移動consumer對象插入函數體,並返回對它的引用,就像這樣:

commonServicesModule.service('getDetails', ['$http', 'urlc', function($http, urlc) { 

    var getConsumer = { 
     profile: function(id) { 
      return $http({ 
       url: urlc.details.consumer.profile, 
       cache: true, 
       params: { 
        'consumer_id': id, 
        'hello': id, 
       }, 
       method: 'GET', 
      }).success(function(result) { 
       return result; 
      }); 
     } 
    }; 

    return { consumer: getConsumer }; 

}]);