Just started diving into Angular and javascript. I've constructed a controller that uses a factory service to retrieve data from a local JSON file. The majority of my code is inspired by, or directly copied from this article by Dan Wahlin. I'm having trouble accessing the $scope.books variable outside of the function and can't seem to pinpoint the issue. When I use console.log inside the function, I see the desired object, but it returns undefined when used outside. Any suggestions on what might be going wrong here? Thank you.
app.controller('FormController', ['$scope', 'tocFactory', function ($scope, tocFactory) {
$scope.books;
getBooks();
function getBooks() {
tocFactory.getBooks().
success(function(data, status, headers, config) {
$scope.books = data;
console.log($scope.books);
}).
error(function(data, status, headers, config) {
// log error
})
}
console.log($scope.books);
}]);