angularjs - Angulajs: Share http response with all pages without loading 2nd time -
i hope thread finds healthy.
my app has 2 pages , using same controller both pages.
i doing dozen of http requests @ page load , share data both pages. each time visit 1 of pages http requests sent server again. how can request server 1 time , share data between pages.
i tried create factory 1 of http requests still request sent each time visit 1 of pages.
my code:
app.controller('maincontroller', function ($scope, translation) { //methos 1 $http.get('types/'). success(function (data, status, headers, config) { $scope.types = data; }); //methos 2 translation.list(function(tr) { $scope.trans = tr; }); } app.factory('translation', function ($http) { return { list: function (callback) { $http.get('trans/').success(callback); } };
});
what doing wrong here?
heres example:
app.factory('translation', function ($http,$q) { var types_q = $q.defer(); var service = { types: [], list: list } return service; function list() { if (!service.types.length) { console.log('fetching server...'); $http.get('trans/').then(function(response) { service.types = response.data.types; types_q.resolve(response); }); } return $q.when('types_q').then(function() { return types_q.promise; }); } }); app.controller('maincontroller', function ($scope, translation) { $scope.loading = true; translation.list().then(function(response) { $scope.types = response.data.types; $scope.loading = false; }); });
Comments
Post a Comment