javascript - Using a promise in Angular -


i think need (but i'm not sure) use promise in angularjs app. following code within controller , calls .service (not .factory if that's relevant?) called processstring.

//complete first var item = magicboxservice.processstring(str);   //and       $scope.task.items.push({ content_type: item.content_type, provider: item.provider, front: item.front, data: item.data }); $scope.save = true; 

the service needs communicate 3rd party api (as own) data. happens item variable empty when next part of code executed.

i've tried $timeout on api call doesn't seem work thought maybe promise need use i've tried following:

var item = magicboxservice.processstring(str).then(function() {      $scope.task.items.push({ content_type: item.content_type, provider: item.provider, front: item.front, data: item.data });     $scope.save = true; }) 

but gives me undefined not function. advice/code appreciated.

edit here edited version of .service.

this.processstring = function(str) {    ...    oembedservice.query({url: str}, function(response) {     item.content_type = "image";     item.provider = "flickr";     item.data = response.content.url;     item.front = $sce.trustasresourceurl(item.data);   })     ...    return item }; 

if processstring() function looks this:

function(str) {     var promisemanager = $q.defer();      ...      oembedservice.query({url: str}, function(response) {         var item = {};         item.content_type = "image";         item.provider = "flickr";         item.data = response.content.url;         item.front = $sce.trustasresourceurl(item.data);          promisemanager.resolve(item);     })      ...      return promisemanager.promise; }; 

then can call this:

magicboxservice.processstring(str).then(function(item) {      $scope.task.items.push({ content_type: item.content_type, provider: item.provider, front: item.front, data: item.data });     $scope.save = true; }) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -