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); 

and that's see in console: image

(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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -