gpt4 book ai didi

angularjs - 在 CRUD 服务中使用 cacheFactory

转载 作者:行者123 更新时间:2023-12-01 18:03:46 25 4
gpt4 key购买 nike

我想为应用程序创建原型(prototype),但不想调用其余服务来检索数据。为此,我想在服务(缓存)中创建我的对象并使用这些服务来显示对象。

我会在服务中使用 CacheFactory,对于每个 crud 操作,直接操作缓存中的对象数组。

现在我有以下代码:

缓存服务:

services.factory('TeacherCache', ['$cacheFactory', function ($cacheFactory) {
var cache = $cacheFactory('TeacherCache');
cache.put('teachers', [
{
id: 1,
clientId: 100,
username: 'teacher465462@gmail.com',
password: 'mdp',
lastName: 'THETEACHER2',
firstName: 'DE',
secondFirstName: '',
address: '9, ADRESS',
city: 'Paris',
country: 'USA',
zipCode: '7ffd5445020',
email: 'my.email@gmail.com',
homePhone: '006546546546',
cellPhone: '54415151661',
birthDate: '31/12/1958',
enabled: true,
roles: [
{id: 1, name: 'Teacher'}
],
//---------------------
substitute: false,
schoolClasses: [],
subjects: [],
options: [],
employedDate: '28/08/2013'
}
]);

return cache;
}]);

还有我的对象服务:

services.factory('TeacherService', 'TeacherCache', function (TeacherCache) {
return {
errorMessage: '',
create: [...],

get: function (id) {
[...]
},

//Get a list of db objects with query
query: function () {
return TeacherCache.get('teachers');
},

delete: [...]
};
})
;

但是当我在 Controller 中注入(inject) TeacherService 并调用查询时:

var teachers = TeacherService.query();

我有以下错误:

Error: Argument 'fn' is not a function, got string

我该如何解决这个问题?谢谢!

最佳答案

“TeacherService”中的第二个参数应该是一个数组。

controllers.factory('TeacherService', ['TeacherCache', function(TeacherCache){
return {...}
}]);

希望对您有所帮助,这是一个 plunker与它一起工作。

关于angularjs - 在 CRUD 服务中使用 cacheFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19016260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com