Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to pass a javascript variable to a controller angular and I can not do it, any suggestions?

My function:

function coordenadas(position) {
            var latitud = position.coords.latitude;
            var longitud = position.coords.longitude;
        }

My controller:

'use strict';

/* Controllers */

var moduloMapa = angular.module('BeLiga.indexMapControllers', ['google-maps']);

moduloMapa.controller('controlIndexMap', function($scope) {
    obtener_localizacion();
    $scope.center = {
        latitude: 45,
        longitude: -73
    };

    $scope.zoom = 8;
});

I need to get the value of these two variables of function "coordenadas":

 var latitud;
 var longitud;
share|improve this question
    
What's the definition of obtener_localizacion()? This function is being used, but is never defined in the code you've shown. Also, you must remember that latitud and longitud are variables that exist only in the scope of the coordenadas function. –  user1620696 Apr 27 '14 at 23:21
1  
I suggest you look into the factory, service, constant and value convenience functions on the angular module object. –  Robert Westerlund Apr 27 '14 at 23:25
    
If latitud and longitud are available in a parent scope you have access to them through inheritance. If not, the angular way is to create a service that will provide these values to your controller. Absolutely DO NOT even think about using $rootScope –  mortsahl Apr 27 '14 at 23:29

2 Answers 2

If variables exists on the page you should be able to used them with no problem as long they are define on the right scope.

Meaning that you should do something like this instead:

var latitud =0;
var longitud =0;

function coordenadas(position) {
   latitud = position.coords.latitude;
   longitud = position.coords.longitude;
}

What I would do probably in a case like this is to create a callback function instead:

function coordenadas(position, callback) {
  var latitud = position.coords.latitude;
  var longitud = position.coords.longitude;

  callback(latitud,longitud);
}

In the controller:

moduloMapa.controller('controlIndexMap', function($scope) {
    var somePositionObject = //not specified on the demo code.

    //invoking method with callback function to get both values.    
    coordenadas(somePositionObject,function(latitude, longitude){
      $scope.center = {
        latitude: latitude,
        longitude: longitude
      };
    });

    $scope.zoom = 8;
});
share|improve this answer
2  
It's generally not a good idea to make yourself dependant on global variables. In the Angular world, this should probably be solved using a factory, service, constant or value provider. –  Robert Westerlund Apr 27 '14 at 23:26
    
@robert.westerlund I totally agree, I was just trying to make a point on that. I just updated the answer with the approach I would personally follow but it took me longer to write –  Dalorzo Apr 27 '14 at 23:30

You could probably put the coordenadas function in a service. Services, in some ways, are simply like modular wrappers for "global" functions, without having to use global functions (and you can just inject the service into whatever controller you want.)

For example:

moduloMapa.service('coordenadas', function() {
    return function(position) {
        return {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude
        }
    }
});

moduloMapa.controller('controlIndexMap',['coordenadas', function(coordenadas) {
    var latitude = coordenadas(/* whatever position */).latitude;

    // Now you've injected the 'coordenadas' service
    // You can dynamically return a latitude/longitude (in the controller's -- not global -- scope)
}]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.