如何从已解决的延迟Angularjs中获取价值

时间:2016-09-07 06:46:24

标签: javascript angularjs ionic-framework

我正在使用ionic和angularjs创建android应用程序,并使用window.requestFileSystem从存储中获取图像。因此,对于此过程,我使用$q.defer()进行异步处理,并且其工作正常并在控制台中显示对象:console.log(deferred.promise)

enter image description here

但是现在,当我试图从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);

由于

1 个答案:

答案 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
});