Habchi
Habchi

Reputation: 1971

AngularJs resolving service from controller with ngResource

I have structured my AngularJs application in a way where I have a factory that fetch data from a back-end using ngResource as a dependency. This factory is injected in a Service, and finally this service is injected in a controller.

The problem is that since my Back-end calls take some time, results are not reflected in the controller. I do manage the promise in the service and it does return a result after some time 'cause I'm able to print it in the console, and I really want to avoid this promise handling in the controller, also the $q usage is now deprecated so I want to know if there's a more fancy solution more like Angular(2/4) that exists for AngularJs to resolve this problem.

Thank you

Upvotes: 0

Views: 37

Answers (1)

HollyPony
HollyPony

Reputation: 847

I can't see another ways to use Promises (with angularjs $q) since it's in the core of AngularJS.

I found the doc explicit enough about this point : ngResource. However, ngResource is a wrapper of $http which give you a promise. In an other hand, ngResource provide you an easy way to bypass this step with a callback shortcut.

If you want a fancy solution in angularjs, you should stop usage of $resource / $http, and take a look at fetch and async await concepts.

Anothers solutions exists but it's some very old school ways to block your main thread.

NB: I'm very curious about the deprecation of $q and the fancy ways of Angular you quoted.

Upvotes: 1

Related Questions