How to make angularJS controller be active at all times?

ساخت وبلاگ

Vote count: 0

I am developing an angularJS application where my geocordinates from the device are taken and sent to my REST service every few seconds. Code for geo coordinates sending is located in just one controller. It means that geolocation is only updated in my server if the template page for that controller is open. How do I make it so that geolocation is always updated regardless of which page I am on.

Here is my code for that controller:

.controller('geolocationCtrl', [ '$scope', '$http', 'geolocationFactory', function($scope, $http, geolocationFactory) {

 // constants should be uppercase var GET_PATH = 'https://temp-domain.co.uk/rest/rest.php/client/geolocation', PUT_PATH = 'https://temp-domain.co.uk/rest/rest.php/client/geolocation/update'; $http.get(GET_PATH).then(function (response) { var respLong = Number(response.data.longitude), respLat = Number(response.data.latitude); $scope.map = drawMap(respLong, respLat); $scope.marker = new google.maps.Marker({ position: new google.maps.LatLng(respLong, respLat) }); $scope.marker.setMap($scope.map); }, function (errorResponse) { console.error('error response ' + errorResponse); }); function drawMap(latitude, longitude) { var lat = latitude, long = longitude, currentLatlng = new google.maps.LatLng(lat, long); var map = new google.maps.Map(document.getElementById('map'), { center: currentLatlng, zoom: 10 }); return map; }; setInterval(function () { geolocationFactory.getCurrentPosition().then(function(location) { var newLatitude = location.coords.latitude, newLongitude = location.coords.longitude; // delete old marker $scope.marker.setMap(null); $scope.marker = new google.maps.Marker({ position: { lat: newLatitude, lng: newLongitude } }); var LatLng = new google.maps.LatLng(newLatitude, newLongitude); $scope.map.setCenter(LatLng); $scope.marker.setMap($scope.map); // send data to server $http.put(PUT_PATH, { "longitude": newLatitude, "latitude": newLongitude }).then(function (response) { console.log('data sended'); }, function (errorResponse) { console.error(errorResponse); }); }.bind(this)); }, 2000); }])
asked 30 secs ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : how to make slime,how to make money,how to make french toast,how to make a resume,how to make buttermilk,how to make money online,how to make pancakes,how to make jello shots,how to make ice cream,how to make fried rice, نویسنده : استخدام کار backsoft بازدید : 386 تاريخ : شنبه 30 مرداد 1395 ساعت: 22:24