Promise Object Not Resolving Outside of Angular Function

159 views
Skip to first unread message

Kevin Sheppard

unread,
Jan 24, 2013, 5:52:48 PM1/24/13
to ang...@googlegroups.com
Hi all,

My question is already posed in this fiddle here: http://jsfiddle.net/kshep92/cSKz3/7/ but here is some background. I am trying to resolve a promise object inside of a service before my view is loaded. The problem is that the promise will not resolve (or it will resolve but not communicate this with Angular) unless the resolve() method is called inside of an angular ($) function or on its own as part of the main body of a service method. 

E.g.

function res() {
 console.log("Res called");
 deferredObject.resolve();
}
res();

Will work, but doing

setTimeout(res, 5000)

will not.

From the documentation I understand that in order for it to work, I have to wrap the resolve() call inside of a $scope.$apply function, but it get's tricky when i'm relying on a callback function from jQuery or WebSQL to resolve the promise and i'm inside a service object (see the fiddle for clarification). I understand that injecting $rootScope into a service is bad practice and is messy, so what other methods are there to achieve what i'm trying to do?

Peter Bacon Darwin

unread,
Jan 24, 2013, 6:13:03 PM1/24/13
to ang...@googlegroups.com
If you are just injecting it to run $apply that is not a bad thing.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Kevin Sheppard

unread,
Jan 25, 2013, 4:20:00 PM1/25/13
to ang...@googlegroups.com
Hmm...Ok, so what's an example of a bad use-case?


To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.

Joshua Miller

unread,
Jan 25, 2013, 4:23:27 PM1/25/13
to angular
Hello!

For example, injecting $rootScope into a service to set a variable on it would (in general) be bad juju.

Josh

Kevin Sheppard

unread,
Jan 25, 2013, 4:30:29 PM1/25/13
to ang...@googlegroups.com

Ooohh.. Now I see. Well thank you so much for the help. Now I don't feel like such a dirty coder :-)

Reply all
Reply to author
Forward
0 new messages