geolocation - How to use AngularJs to load user location, which can be used in the entire application? -
in angularjs, after call geolocation , call remote service determine location (city, state, country), how can reuse data in entire application?
what mean is, getting geolocation , determining city async, ideally, want things once. after that, every time new page opened, controller asking geoservice city or something.
but how can it?
i have tried create service, exposes property, city. in next controller, property has no value, , have recall methods. thought service should singleton.
i could, use cookie after first time loaded.
but want know, how other people handle kind of thing?
thanks!
you should sharing service data across multiple controllers, that:
app.service('cache', function ($http, $q) { var mycache={}; return { getdata: function (key) { var deferred = $q.defer(); if (mycache[key]) { deferred.resolve(mycache[key]); } else { $http.get('api/your_location').then(function (data) { mycache[key] = data.data; deferred.resolve(mycache[key]); }); } return deferred.promise; } } }); app.controller('test', function ($scope, cache) { cache.getdata('cache').then(function (data) { $scope.data = data; }); }); app.controller('test1', function ($scope, cache) { //since data cached server cached data cache.getdata('cache').then(function (data) { $scope.data = data; }); });
Comments
Post a Comment