AngularJS - 我在这个 Factory 注入中遗漏了什么吗?
AngularJS - Am I missing something in this Factory injection?
我正在使用 $cacheFactory 将一些数据保存在我的缓存中,一切都运行良好,直到今天我决定将我的 cacheFactory 分成一个名为 class 的 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;
}
tableCacheFactory
包裹在 my-cache-factory
模块中。因此,您需要先将模块注入 my-component
模块,然后再使用它。所以应该是这样的:
angular.module('my-component', ['my-cache-factory'])
您在模块 my-cache-factory 中定义了缓存工厂,但从未将该模块注入到您的主要组件服务模块中。改为 angular.module('my-component', ['my-cache-factory'])
。
您似乎对 $cacheFactory
的工作原理有一些误解。
在 var dataCache = tableCacheFactory.get('tablecache');
中,您使用它就像它是一个包含另一个缓存对象的初始化缓存对象。
另一方面,dataCache = tableCacheFactory('tablecache');
使用它就像 $cacheFactory
本身一样。
并且他们都尝试访问记录 'tablecache',我认为应该已经是 tableCache 本身。
错误正是它所说的。 As per the docs,调用$cacheFactory('my-cache');
并没有return创建更多缓存的函数。
它 return 是一个 $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);
}
我正在使用 $cacheFactory 将一些数据保存在我的缓存中,一切都运行良好,直到今天我决定将我的 cacheFactory 分成一个名为 class 的 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;
}
tableCacheFactory
包裹在 my-cache-factory
模块中。因此,您需要先将模块注入 my-component
模块,然后再使用它。所以应该是这样的:
angular.module('my-component', ['my-cache-factory'])
您在模块 my-cache-factory 中定义了缓存工厂,但从未将该模块注入到您的主要组件服务模块中。改为 angular.module('my-component', ['my-cache-factory'])
。
您似乎对 $cacheFactory
的工作原理有一些误解。
在 var dataCache = tableCacheFactory.get('tablecache');
中,您使用它就像它是一个包含另一个缓存对象的初始化缓存对象。
另一方面,dataCache = tableCacheFactory('tablecache');
使用它就像 $cacheFactory
本身一样。
并且他们都尝试访问记录 'tablecache',我认为应该已经是 tableCache 本身。
错误正是它所说的。 As per the docs,调用$cacheFactory('my-cache');
并没有return创建更多缓存的函数。
它 return 是一个 $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);
}