This code is used to get the users location and deliver it as a resource to the controller. The aim is to have this data shared across multiple controllers in the application. So far this does deliver the data as a parameter of the then()
method on the promise. Is this pattern a suitable way of sharing a resource which is delivered through a promise? Any feedback would be greatly appreciated.
angular.module('geolocation', [])
.factory('geolocation', function ($q, $window) {
return {
getLocation: function () {
var deferred = $q.defer();
$window.navigator.geolocation.getCurrentPosition(function(position){
deferred.resolve(position);
});
return deferred.promise;
}
};
});
angular.module('nearestLocationCtrl', [
'geolocation'
])
.controller('NearestLocationCtrl', ['$scope', 'geolocation', function ($scope, geolocation) {
geolocation.getLocation().then(function (data) {
console.log(data);
});
}]);