0

$cacheFactory を使用してデータをキャッシュに保存していますが、今日まですべてがうまく機能していたので、cacheFactory を MyFactory.js という単一のクラスに分離することにしました。今、私はエラーが発生しています:

TypeError: tableCacheFactory is not a function

単純な方法か何かとして注射を取っているので、ここに何か欠けているかどうか誰か知っていますか?

main.js

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

MyFactory.js

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

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

MyService.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

仕組みについて誤解しているようです$cacheFactory

var dataCache = tableCacheFactory.get('tablecache');別の Cache オブジェクトを含む初期化された Cache オブジェクトのように使用しています。

一方、それ自体dataCache = tableCacheFactory('tablecache');のように使用し$cacheFactoryます。

そして、どちらも、すでにtableCache自体であるはずのレコード「tablecache」にアクセスしようとしています。

エラーはまさにそれが言っていることです。docs に従って、呼び出し$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');
    }
]);

そして、より奇妙な「テーブルキャッシュ」名前空間を実行する必要なく、それを使用します

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