我正在尝试从本地存储中获取一些数据,但我想使用一次就绪
的日期。像这样的东西:
// this belongs to a Helper factory
getData: function(){
var deferred = jQuery.Deferred();
setTimeout(function(){
var data = {
description: localStorageService.get('description'),
};
deferred.resolve()
}, 10);
return deferred.promise();
},
// I set it up so that in the controller I can do
// $scope.data = Helper.getData();
与我的示例类似,我只想返回 data
对象,一旦它具有来自 localstorage 的 description
我正在使用 angularjs,所以我想在 promise 得到解决后在我的 View 中显示 {{data}}
。比如可能使用 ng-hide
或类似的东西。
无论如何,我的问题是关于建立该 promise 。
有什么想法吗?
最佳答案
$timeout() 返回一个 promise ,所以这是可行的:
getData: function(){
return $timeout(function(){
return localStorageService.get('description'),
},3000);
}
用法:
getData().then(function(data){
console.log(data);
})
关于javascript - 如何使用 angular 和 JavaScript promise 返回一些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25174299/