javascript - AngularJS controller does console.log() before service -
i developing app in m.e.a.n stack. i'm using angular controller (from directive), , service. controller calls service, service sends request nodejs , gets result. log result in service , data.
the problem controller logs service before controller gets results.
in services.js:
var self = this; self.show = function() { $http.get('/contactlist').success(function(response) { console.log(response); return response; });
in directives.js:
this.contacts = contactssrv.show(); console.log(this.contacts);
(the directive logs before gets results contactssrv.show()
)
how can make contactssrv.show()
asynchronous?
thanks!
using .then promise return
service.js
var self = this; self.show = function() { return $http.get('/contactlist'); };
directive.js
contactssrv.show() .then(function (response) { console.log(response); this.contacts = response; });
Comments
Post a Comment