【发布时间】:2014-02-17 06:00:45
【问题描述】:
我想在 AngularJs 中模拟一个 async db 操作。 (通过setTimeout)。
我有这个工厂功能代码:(jsbin)
它使用内部 array :
shoppingModule.factory('Items', function() {
var items = {};
items.query = function() {
// In real apps, we'd pull this data from the server...
return [
{title: 'Paint pots', description: 'Pots full of paint', price: 3.95},
{title: 'Polka dots', description: 'Dots with polka', price: 2.95},
{title: 'Pebbles', description: 'Just little rocks', price: 6.95}
];
};
return items;
});
controller,依次调用:
function ShoppingController($scope, Items) {
$scope.items = Items.query();
}
但我想通过setTimeout 操作将数据的返回模拟为异步操作。 (简而言之 - 我想添加非阻塞延迟)
我尝试了什么?
我尝试使用 $timeout 变量但没有成功。也尝试使用 setTimeout 没有成功(因为我在方法中没有 $scope 对象。)
问题:
如何在工厂函数中添加 setTimeout(3 sec 模拟 db 操作)?
【问题讨论】:
标签: javascript angularjs