gentos gentos - 5 months ago 10
jQuery Question

angularjs expose service property to $scope

I am trying to expose an angular service property to $scope, to use it in the view my code looks like this

/**API Post SERVICE for user login*/
function login(username,password){
return $http.post('/api/login/',{
username:username,
password:password
}).then(loginSuccessFn, loginErrorFn);

function loginSuccessFn(data, status, headers, config){
Authentication.setAuthenticatedAccount(data.data);

window.location = '/';
}
function loginErrorFn(data, status, headers,config){
console.error("Epic Failure");
}
}


so when the $http method returns error and the function loginErrorFn() is called, the response data to be passed in the view, so I may raise an bootstrap alert

How can i achieve this ?

Answer

@Karim was right, just something missed there:

when you inject $rootScope inside the service:

 $rootScope.$emit('httpCall', 'success');
 $rootScope.$emit('httpCall', 'failure');

and when you catch it the:

$rootScope.$on('httpCall', function(e,p){scope.isSuccess = p;})

The $rootScope.$on('httpCall', function(e,p){} has two parameteres