Studying and testing a bit AngularJS, and I have a doubt about how to structure a service to make some data available to some controllers.
I have a JSON file, and I would like to load it from my service and then use this service through my controllers to access the data.
Right now my service looks like this:
dashboardController.factory ('peopleRepository', function($http) {
return {
getAllPeople: function() {
return $http.get("people.json");
}
}
});
Easy and straightforward. Then in my controller, I do something like this:
dashboardController.controller ('genderCtrl', function ($scope, $rootScope, peopleRepository) {
$scope.males = 0;
$scope.females = 0;
$scope.people = peopleRepository.getAllPeople().success(function(users) {
console.log ("SUCCESS -< ", users);
});
});
...and of course everything it's fine and works. But I am pretty sure this is not the right approach. I would need to access this data in several controllers, and this means that basically the file will be loaded over and over, and every time I am waiting for the success event to load the content. Not really elegant. Any suggestion/hint for a newbie?