I have a following code where controller use factory function to get user data via an api request.
controller
myApp.controller('UserApiCtrl', function($scope,$auth,Account){
$scope.getProfile = function(){
Account.getProfile()
.then(function(response){
$scope.user = response.data;
})
.catch(function(response){
// errors handled here
})
};
$scope.getProfile();
})
factory
angular.module('MyApp')
.factory('Account', function($http){
return {
getProfile: function(){
return $http.get('/api/me');
}
}
});
Now in controller when I console.log(response.data), json data is available, but when I console.log($scope.getProfile()) it's undefined.
Thanks!!
.thenblock is going to happen in the future, anything outside happens now, You can't log now something that you expect to happen in the future.$scope, when the future event finishes ($scope.useris populated with a value), then angular will run a$digestand update the DOM.