angularjs - Constantly running service while in app -


i developing app pushes user's geolocation server (with regards permissions, of course).
currently, when user navigates account page, method invoked push geolocation server.
instead, service (if that's best method) run while app running push geolocation. means location held user date , not updated when account page visited.

there checkbox on account page select if want share geolocation or not.
i have $watcher on checkbox...

$scope.$watch("account.sharelocation", function(newvalue, oldvalue) {     if (newvalue) {         locationservice.pushlocation(userposition);     } else {         locationservice.pushlocation(null);     } }); 

so, if user selects share location (newvalue === true) then, pushlocation() should invoked userposition until value of account.sharelocation (the checkbox) altered otherwise.

i made small example here: https://jsfiddle.net/ojzdxpt1/2/

you have main controller inits service if user has allowed (i added mock $timeout of them unchecking it).

var app = angular.module('testapp', []);  app.controller('testcontroller', function($scope, $timeout, locationservice) {   console.log('app init');    //-- on checkbox change and/or app init;   var tracklocation = true;   if (tracklocation) {     locationservice.start();      //-- imitate them turning service off     $timeout(function() {       console.log('stop tracking location');       locationservice.stop();     },10000);   }   }); 

now service this:

app.service('locationservice', function($interval) {   var int;   return {     start: function() {       int = $interval(this.savelocation,3000);     },     stop: function() {       $interval.cancel(int);     },     savelocation: function() {       console.log('save location');     }   } }); 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -