Search code examples
angularjsangularjs-directiveleafletangular-leaflet-directive

angular leaflet custom markers (using angular directives)


I'm trying to create a "leaflet marker" using an angular directive. For design purposes, we separate the presentation and the model so different persons can work on different parts of the application. My issue, more likely, is more a "scope" problem than a "leaflet" problem. I'm trying to pass an object to be used in the angular directive, while I'm adding the markers on "$scope", in the controller. That directive, "" in my app, is the only tag in my "message" property on each marker object to be presented in the map. It has an attribute "estacao" which in portuguese is the same as "station".

So, my code is here:

        angular.forEach($scope.estacoes, function(estacao) {
            $scope.markers.push({
                lat: estacao.latitude, 
                lng: estacao.longitude, 
                message: "<popup estacao='estacoes[" + i + "]'></popup>"
            });
            i++;
        });

http://plnkr.co/edit/evaQpqGZUz39Y7MNqbo7?p=preview

The problem seams to be that my "estacao" is null when the directive is processed.

Can anyone help me to figure out what is happening?


Solution

  • The 'auto' compile of the popup message (from the leaflet directive) uses the root scope. So you need to assign your response estacoes to the root scope:

    promiseEstacoes.then(function(estacoes) {
       $rootScope.estacoes = estacoes.estacoes;
       ...
    }
    

    http://plnkr.co/edit/OkQcth2zNrEdO2rgwBv8?p=preview