I am working on a mobile application using AngularJS as a framework, I currently have a structure similar to this:
app.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/', { templateUrl : 'pages/home.html', controller : 'homeCtrl' }) .when('/one', { templateUrl : 'pages/one.html', controller : 'oneCtrl' }) .when('/two', { templateUrl : 'pages/two.html', controller : 'twoCtrl' }); }]); app.controller('homeCtrl', ['$scope', function($scope) { }]); app.controller('oneCtrl', ['$scope', function($scope) { }]); app.controller('twoCtrl', ['$scope', function($scope) { }]);
And then I show the content using ng-view :
<div class="ng-view></div>
Everything works fine, but I need to load the data from the JSON file to populate all the application content. I want to make an AJAX call only once , and then transfer the data through all my different controllers. In my first attempt, I decided to create a Service with $http.get() inside it and include it in each controller, but it doesnβt work, because it makes another ajax request every time I enter and use the service. Since I am new to using angular, I am wondering what is the best way or even more so "angular" to achieve this without messing it up.
Edit: I am adding a service code, which is a simple request to $http.get :
app.service('Data', ['$http', function($http) { this.get = function() { $http.get('data.json') .success(function(result) { return result; }) } });