我正在使用ionic和angularjs创建android应用程序,并使用window.requestFileSystem从存储中获取图像。因此,对于此过程,我使用$q.defer()
进行异步处理,并且其工作正常并在控制台中显示对象:console.log(deferred.promise)
但是现在,当我试图从deferred.promise
获取值
var vals = deferred.promise;
console.log(vals.$$state.value);
显示undefined
。谁能告诉我如何才能获得这个价值?
我的代码:
var deferred = $q.defer();
document.addEventListener("deviceready", function() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
imagePath = fileSystem.root.toURL() + 'SmaartMedia/' + splited[splitedLength-1];
deferred.resolve(imagePath);
$rootScope.$apply();
});
}, false);
var vals = deferred.promise;
console.log(vals.$$state.value);
由于
答案 0 :(得分:4)
function getStuff() {
var deferred = $q.defer();
document.addEventListener("deviceready", function () {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
imagePath = fileSystem.root.toURL() + 'SmaartMedia/' + splited[splitedLength - 1];
deferred.resolve(imagePath);
$rootScope.$apply();
});
}, false);
return deferred.promise;
}
//Get value like this
getStuff().then(function(response){
//Use value here
});