Javascript 使用angularJS智能表
我想为我的网站使用AngularJS智能表。我已经阅读了文档()。很难理解app.factory在这里是如何工作的。我想知道如何为数据库(mongodb)中的数据实现createRandomItem函数Javascript 使用angularJS智能表,javascript,angularjs,smart-table,Javascript,Angularjs,Smart Table,我想为我的网站使用AngularJS智能表。我已经阅读了文档()。很难理解app.factory在这里是如何工作的。我想知道如何为数据库(mongodb)中的数据实现createRandomItem函数 app.factory('Resource',['$q','$filter','$timeout',函数($q,$filter,$timeout){ //这将是调用服务器的服务,服务器是模型和$http之间的标准桥梁 //数据库(通常在服务器上) var randomsItems=[]; 函数c
app.factory('Resource',['$q','$filter','$timeout',函数($q,$filter,$timeout){
//这将是调用服务器的服务,服务器是模型和$http之间的标准桥梁
//数据库(通常在服务器上)
var randomsItems=[];
函数createRandomItem(id){
var heroes=[‘蝙蝠侠’、‘超人’、‘罗宾’、‘雷神’、‘绿巨人’、‘尼基·拉森’、‘斯塔克’、‘鲍勃·勒庞’];
返回{
id:id,
名称:英雄[Math.floor(Math.random()*7)],
年龄:Math.floor(Math.random()*1000),
保存:Math.floor(Math.random()*10000)
};
}
对于(变量i=0;i<1000;i++){
randomsItems.push(createRandomItem(i));
}
//对服务器的假调用,通常此服务将序列化表状态以将其发送到服务器(例如使用查询参数)并解析响应
//在我们的例子中,它实际上执行服务器中发生的逻辑
函数getPage(开始、编号、参数){
var deferred=$q.deferred();
var filtered=params.search.predicateObject?$filter('filter')(randomsItems,params.search.predicateObject):randomsItems;
if(params.sort.predicate){
filtered=$filter('orderBy')(filtered,params.sort.predicate,params.sort.reverse);
}
var result=filtered.slice(开始,开始+编号);
$timeout(函数(){
//注意,服务器传递有关数据集大小的信息
推迟,解决({
数据:结果,
numberOfPages:Math.ceil(filtered.length/number)
});
}, 1500);
回报。承诺;
}
返回{
getPage:getPage
};
}]);
好的。。。我的闪耀时刻D只是开玩笑。。你的回答是贝娄
如果你熟悉angular factory,这是一个相当直接的例子
当您使用工厂服务时,它会执行工厂定义中的代码,并返回您想要调用函数的任何内容
因此,上面的代码所做的是,当您使用此factory服务时,只需在数组randoments
中生成一个随机项列表(复仇者),这一步相当简单。如果查看createRandomItem(id)
和它后面的for
循环
然而,诀窍在于getPage()
和资源工厂返回的内容。所以,让我们去旅行吧
在调用Resource.getPage()
时使用Resource
工厂的代码中,它将返回一个promise
对象,您可以在该对象上调用其他JS函数。在getPage()
内部,您可以看到它设置了一个timeout
来调用resolve
对象,该对象中包含变量data
和numberOfPages
,该对象在defered
对象的promise
上触发doneCallback
所以当你像
Resourse.getPage(1,2,3) // please see the use of these arguments inside the getPage function
.done(function(result){
result.data; //the fake server data from the factory
result.numberOfPages; //this is computed in factory as well
});
当1500ms通过时,将触发传递给done的函数/回调
总结与回答
注意:请先阅读上面的内容,我花了很长时间才写出来。
现在来解决你的问题。你能做的就是修改这个
app.factory('Resource', ['$q', '$filter', '$timeout', function ($q, $filter, $timeout)
到
使用$http
从服务器检索数据
,或使用mongodb
从服务器中填充数据
$http.get(server_url).success(function(response){
//....do something with the response from the server like filtering etc..
//finally..
deferred.resolve({
data: response
});
});
使用服务时的最后一个问题
Resourse.getPage(1,2,3) //what ever you want to pass its not necessory to pass the same as above
.done(function(response){
//do what ever you want to do with response from your factory.. PHEW...
});
这是我迄今为止给出的最长的答案。。呸:P
备注1.请随时在评论中提出任何问题谢谢您的回答。现在我明白了
$http.get(server_url).success(function(response){
//....do something with the response from the server like filtering etc..
//finally..
deferred.resolve({
data: response
});
});
Resourse.getPage(1,2,3) //what ever you want to pass its not necessory to pass the same as above
.done(function(response){
//do what ever you want to do with response from your factory.. PHEW...
});