gh9 gh9 - 6 months ago 22
Javascript Question

returning angular promise from function

app.factory('myService', ["$http",function($http){
this.test = function(){
return $http.post('fooBar.com');
}
}

app.controller('myController' ['myService',function(mySerivce){
myService.test().then(function(){ console.log("success");});
}]);


My issue is, when I call
myService.test()
in
myController
. Success is never being outputted to the console. What am I doing wrong and why?

Answer

The following block of code contains an error

myService.test().then(function() console.log("success"););

This is the correction. You were missing the {} to wrap the contents of your function inside then()

myService.test().then(function() { console.log("success"); } );