javascript - I can't obtain service variable angularjs -
This is a service for getting my controller and latitude and graphs.
var moduloMapa = angular Module ('Beliga. Index map controller', ['google-maps']); moduloMapa.controller ( 'controlIndexMap', function ($ scope, mapService) {$ scope.center = {Latitude: 30, Longitude: 20}; $ scope.zoom = 8; $ scope.latitud = mapService.localizar ();} ); moduloMapa.service ( 'mapService', function () {this.localizar = function () {navigator.geolocation.getCurrentPosition (mostrar);}; function mostrar (Posicion) {/ * Posicion es el parametro Tiene que los Valores, Las variable Para Latitud y longitud Las obtenemos del parametro posicion.coords * / var lat = posicion.coords.latitude; // obtengo La Latitud bridesmaid Longitude = posicion.coords.longitude; // obtengo la longitud warning ( "te he encontrado, estas en : "+ Latitude +", "+ lane"); return "latitude;}}});
The problem is: when I see the scope variable" $ scope.latitud "in the browser So this is empty and I do not know why.
How do I latitud And long-term $ scope controller?
I angled my early JS, thank you for your attention.
the problem getCurrentPosition asynchronous runs.
such try it
moduloMapa.service ( 'mapService', function () { This.localizar = function () {var coords = {latitude: 0, longitude: 0}; Navigator.geolocation.getCurrentPosition (function (posicion) {cords.lat = posicion.coords.latitude; cords.lon = posicion.coords.longitude;}); Returned coords;};});
And as soon as the browser goes to the scope of the controller they will be available.
$ scope.latitud = mapService.localizar (). Latitude;
Update:
Sorry for the typo, I've made a working plunker
Comments
Post a Comment