See Aghedkaio video, I noticed a common pattern is to return a custom promise and resolve it
Callbacks.
.factory ( 'myFact', function ($ q, $ http) {returnData: function () {var deferred = $ q.defer (); $ http.get ( '/ Path / To / api ') .success (function (data) {deferred.resolve (data);}); Returns; Promote;}};});
I typically thus write:
.factory ( 'myFact', function ($ http) {return} {getData: function () { Return $ Http.get ('/ path / to / api'). Then (function (race) {return res.data;});}};}); Instead of making a promise to $ http
, do you have any advantage of making promise? Attitudes look the same for me.
No, no benefit, this is the same, if you have created an example in your first code You have applied your solution ()
method to create solved promises .
The same procedure that you know must have asynchronous functions and The future of object to work with Time Angular JS which you will have different values or new data in a few moments the future will know you when that happens, your app interested parties deferred action access to the results May need to be done.
Now when you work with, none of it has already been pledged to a resolve these will return you can apply directly to you things There should not be a different way of doing it and you need to apply a different approach.
But not all angular JS services are going to work for you, for example look at $ resource
, which will be used for use in RESTful Web API scenarios $ Http wraps
$ resource
will not return a solved promise , a promise Yes, you are getting one But you will need the final step of solving this (or else).
How are you doing this, it is good, like this I'm working with $ http
, but the first snippet code is what we will all search for. When we will need to work differently with $ http
or other services with ' at work ' or ' ' AJX Work like that.
No comments:
Post a Comment