So, my AngularJS Factory looks like:
.factory('getCategories', function($http) {
return $http.get('api/categories');
})
Then, in the controller, I am getting the value like this:
getCategories.
success(function(data) {
$scope.category_data = data;
});
In the view, I can access the scope variable via {{ category_data }}, and it works fine, returns JSON. But, when I try to access the variable in the controller, it just is always empty. I tried doing this:
$scope.category = $scope.category_data;
Then, after I had that, I would try to access the $scope.category variable in the view via {{ category }}, and it would be blank.
Any tips on an alternative to what I'm doing? Or what I have done wrong?
EDIT: Here is what my whole controller looks like.
.controller('ForumController', function($scope, $route, $routeParams, $location, $filter, getCategories) {
getCategories.
success(function(data) {
$scope.category_data = data;
});
$scope.category = $scope.category_data;
})
category_datavariable?successhandler?successcallback is indeed async. So if you would like to access the variable, you'd have to do it in thesuccesscallback function. As you can see here in this example, there are two alerts, one outside and one inside the callback. Take note on the order of them going off.