Javascript 调用工厂函数一次

Javascript 调用工厂函数一次,javascript,angularjs,Javascript,Angularjs,我有一个工厂,它被指令多次调用。由于它返回大量数据,因此最后的渲染速度很慢。第二次和第n次调用时,我如何才能只调用一次或将其保存在现金中 appBMdata.factory('Trends',['$http','Config','$q', function($http,Config,$q){ function getAllData() { var source1 = $http.get(Config.api_server + 'bizmo

我有一个工厂,它被指令多次调用。由于它返回大量数据,因此最后的渲染速度很慢。第二次和第n次调用时,我如何才能只调用一次或将其保存在现金中

appBMdata.factory('Trends',['$http','Config','$q',
    function($http,Config,$q){


           function getAllData() {
            var source1 = $http.get(Config.api_server + 'bizmonitor/indicators/get/2016');
            var source2 = $http.post(Config.api_server + 'trends');

            return $q.all([source1, source2]);

          };

          return {
            getAllData : getAllData,
          };     
  }]);

您可以将承诺保存在var中,并在已设置的情况下返回:

appBMdata.factory('Trends',['$http','Config','$q',
  function($http,Config,$q){

         var _cacheGetAllData;
         function getAllData() {
          var source1 = $http.get(Config.api_server + 'bizmonitor/indicators/get/2016');
          var source2 = $http.post(Config.api_server + 'trends');

          _cacheGetAllData = _cacheGetAllData || $q.all([source1, source2]);
          return _cacheGetAllData;
        }

        return {
          getAllData : getAllData,
        };     
}]);
如果希望连续调用强制更新,可以将其编辑为以下内容:

appBMdata.factory('Trends',['$http','Config','$q',
  function($http,Config,$q){
    var _cacheGetAllData;
    function getAllData(ignoreCache) {
      var source1 = $http.get(Config.api_server + 'bizmonitor/indicators/get/2016');
      var source2 = $http.post(Config.api_server + 'trends');

      if (ignoreCache) {_cacheGetAllData = undefined;}
      _cacheGetAllData = _cacheGetAllData || $q.all([source1, source2]);
      return _cacheGetAllData;
    }

    return {
      getAllData : getAllData,
    };     
}]);

是的,您可以将数据保留在
$rootScope
上,并在多次调用时从那里返回数据

appBMdata.factory('Trends',['$http','Config','$q','$rootScope'
    function($http,Config,$q,$rootScope){


           function getAllData() {
            var source1 = $http.get(Config.api_server + 'bizmonitor/indicators/get/2016');
            var source2 = $http.post(Config.api_server + 'trends');

            return $q.all([source1, source2]);

          };
          if($rootScope.data){             // check if data already present
            $rootScope.data=getAllData();    // assign data to rootscope
          }
          return {
           getAllData : $rootScope.data,   //return data from rootscope
          };
  }]);

我在服务中解析它,然后存储数据,如果它有数据,则以承诺的方式返回数据。如果您想再次获取数据,只需添加true作为第一个参数

appBMdata.factory('Trends', ['$http', 'Config', '$q', function($http, Config, $q) {

    var data;

    function getAllData(nocache) {

        var deferred = $q.defer();

        if (data.length && !nocache) {

           deferred.resolve(data);

        } else {

            var source1 = $http.get(Config.api_server + 'bizmonitor/indicators/get/2016');
            var source2 = $http.post(Config.api_server + 'trends');

            $q.all([source1, source2])
                .then(function (values) {
                    data = values;
                    deferred.resolve(data);
                })
                .catch(function (err) {
                    deferred.reject(err);
                });
        }

        return deferred.promise;

    }

    return {
        getAllData : getAllData
    };

}]);

在调用服务Trends.getAllData()时尝试代码更新后,出现错误:“Trends.getAllData不是函数”。我有没有其他的方式来称呼这项服务?@antoniogaj。。您应该只调用
Trends.getAllData
@antoniogaj。。对不起,我打错了。。固定的