I'm trying to return true or false from Laravel to a $scope directive in Angularjs. This is the laravel code that is called:
public function check() {
return Response::json(Auth::check());
}
This is the service that queries Laravel:
app.factory('UserService', ['$http', '$location', function($http, $location) {
return {
loggedIn: function() {
var c = $http.get('/auth/check');
c.success(function(data) {
console.log(data);
return data;
});
}
}
}]);
The response logs fine in the console, but I cant seem to bind it to a $scope directive:
app.controller('BaseController', ['$scope', 'UserService', function($scope, UserService) {
$scope.loggedIn = UserService.loggedIn();
}]);
Returns nothing when I call it as:
{{ loggedIn }}
...in my view.
Any help?
The $http
service returns a promise. So you would have to write something like
app.factory('UserService', ['$http', '$location', function($http, $location) {
return {
loggedIn: function() {
return $http.get('/auth/check');
}
}
}]);
Controller:
UserService.loggedIn().success(function(data) {
$scope.loggedIn = data;
});