I created a factory that returns an $http
promise.
loginRsrc.factory('loginSrvc', ['$http', function($http){
var http = $http({
method: 'POST',
url: "http://myproj.herokuapp.com/api/signIn",
data: $.param({email: "joe@gmail.com", password: "1234567"}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
return http;
I use this factory in a controller. But I'm getting funny behaviour.
When I say
loginSrvc()
.success(function(res){
console.log("res:");
console.log(res);
})
.error(function(res){
console.log("res");
console.log(res);
});
I get an error that TypeError: object is not a function
.
But when I say (omitting parentheses)
loginSrvc
.success(function(res){
console.log("res:");
console.log(res);
})
.error(function(res){
console.log("res");
console.log(res);
});
it works great! The success prints to the console.
I can't understand this weird behaviour. Just adding a success
function calls the $http
activity?
This is my full controller:
loginApp.controller('loginCtrl', ['$scope', '$state', 'loginSrvc', function($scope, $state, loginSrvc){
$scope.loginForm = {};
$scope.loginForm.email = "";
$scope.loginForm.password = "";
$scope.submit = function(){
loginSrvc
.success(function(res){
console.log("res:");
console.log(res);
})
.error(function(res){
console.log("res");
console.log(res);
});
}
}])