简体   繁体   中英

Angular leaflet marker click

I use leaflet with angular and would like to make a button clickable in a message popup. I know I have to compile the HTML, but I can't get it to work in my case since I didn't find an example with a json-request.

Any hint is very welcome!

$http.get(searchterm).then(function(articlesResponse) {

        $scope.geonamesorte = articlesResponse.data;

        var meineMarker = {};

        for ( var i = 0; i < $scope.geonamesorte.length; i++) {
            var ortObjektausListe = $scope.geonamesorte[i];

            var myobjectname = ortObjektausListe.name.replace(/[^a-zA-Z0-9]/g,'_');
            ortlat = parseFloat(ortObjektausListe.lat);
            ortlng = parseFloat(ortObjektausListe.lng);

            meineMarker[myobjectname+i] =  {
                    lat: ortlat,
                    lng: ortlng,
                    message: "<span><a href='' ng-click='dosomething()''>info</a></span>",
                    focus: false,
                    draggable: false
                };

        }

        // Marker on map
        angular.extend($scope, {
          markers: meineMarker,
            defaults:{
              tileLayer:"http://tile.stamen.com/toner-lite/{z}/{x}/{y}.png"
            }
        });

  });

Ok, found the solution here: angular-leaflet-directive custom message html with angular directives in marker popup. How to?

But modified it a bit:

    var myobjectname = 'meinort';

    var meineMarker = {};
        meineMarker[myobjectname] =  {
                    lat: 0,
                    lng: 0,
                    name: 'testname',
                    message: "<span><a href='' ng-click='dosomething()'>info</a></span>",
                    focus: false,
                    draggable: false
                };

  $scope.$on('leafletDirectiveMarker.click', function(e, args) {
            // Args will contain the marker name and other relevant information
            console.log(args);
            var markerName = args.leafletEvent.target.options.name; //has to be set above
            var $container = $(args.leafletEvent.target._popup._container).find('.leaflet-popup-  content'); 
            $container.empty();
            var html = "<p> I am "+markerName +" " + args.leafletEvent.target._popup._content + "</p>", 
              linkFunction = $compile(angular.element(html)),             
              linkedDOM = linkFunction($scope); 
            $container.append(linkedDOM);
        }); 

include a $compile service into controller then write:

meineMarker[myobjectname+i] =  {
                    lat: ortlat,
                    lng: ortlng,
                    message: $compile("<span><a href='' ng-click='dosomething()''>info</a></span>")($scope),
                    focus: false,
                    draggable: false
                };

dosomething() must be defined on the current scope

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM