0

我正在使用 $cacheFactory 将一些数据保存在我的缓存中,直到今天我决定将我的 cacheFactory 分离到一个名为 MyFactory.js 的类中,一切都运行良好。现在我收到错误:

TypeError: tableCacheFactory is not a function

因为它把注射作为一种简单的方法或其他东西,有人知道我是否在这里遗漏了什么吗?

main.js

angular.module('main-component',
    ['my-component',
    'my-cache-factory'
    ]);

我的工厂.js

angular.module('my-cache-factory', [])

    .factory('tableCacheFactory', [
        '$cacheFactory', function ($cacheFactory) {
            return $cacheFactory('my-cache');
        }
    ]);

我的服务.js

angular.module('my-component', [])

    .factory('my-component-service',
        ['$rootScope',
        '$http',
        '$q',
        'tableCacheFactory',
        function ($rootScope, $http, $q, tableCacheFactory) {

            function getMyData (prodNro) {

                var deferred = $q.defer();
                var promise = deferred.promise;

                var dataCache = tableCacheFactory.get('tablecache');

                if (!dataCache) {
                    dataCache = tableCacheFactory('tablecache');  // TypeError: tableCacheFactory is not a function
                }

                var summaryFromCache = dataCache.get('tablecache' + prodNro);

                if (summaryFromCache) {
                    deferred.resolve(summaryFromCache);

                } else { 

                    $http({
                        method: ...
                        data : ...
                        url: ...
                    }).success( function (data, status, headers, config) {

                        var objectResult = {
                            "data": data,
                            "status": status,
                            "headers": headers,
                            "config": config
                        }

                        if (data.response) {
                            // Save to cache
                            dataCache.put('tablecache'+prodNro, objectResult);
                        }

                        deferred.resolve(objectResult);

                    }).error(function (data, status, headers, config) {

                        ...
                    });

                }

                return promise;

            }
4

3 回答 3

0

tableCacheFactory包裹在my-cache-factory模块内。所以你需要先将模块注入到你的my-component模块中,然后再使用它。所以它应该是这样的:

angular.module('my-component', ['my-cache-factory'])
于 2016-05-03T01:16:21.953 回答
0

您在模块 my-cache-factory 中定义了缓存工厂,但从未将该模块注入到您的主要组件服务模块中。angular.module('my-component', ['my-cache-factory'])改为这样做。

于 2016-05-03T01:17:16.973 回答
0

您似乎对工作原理有一些误解$cacheFactory

var dataCache = tableCacheFactory.get('tablecache');您使用它时,它就像一个包含另一个 Cache 对象的已初始化 Cache 对象。

另一方面dataCache = tableCacheFactory('tablecache');,它就像它$cacheFactory本身一样使用它。

他们都尝试访问记录'tablecache',我认为应该已经是tableCache本身。

错误正是它所说的。根据文档,调用$cacheFactory('my-cache');不会返回创建更多缓存的函数。它返回一个$cacheFactory.Cache对象,该对象具有 和 之类put(key, value)的方法get(key)。改用那些。

我会更改缓存的整个结构(注意工厂的名称已更改)

.factory('tableCache', [
    '$cacheFactory', function ($cacheFactory) {
        //Return what the name of the factory implies
        return $cacheFactory('tablecache');
    }
]);

然后使用它而不需要做更多奇怪的'tablecache'命名空间

function getMyData (prodNro) {
    ...
    var summaryFromCache = tableCache.get(prodNro);
    ...
    tableCache.put(prodNro, objectResult);
}
于 2016-05-03T17:26:18.840 回答