2

我突然得到这个,它不允许我正确使用我的传单地图。每次我点击地图时,都会在相同的坐标上添加一个标记。当我尝试使用函数删除标记时,它会清空标记数组,但是标记在地图上仍然可见。这是怎么回事?

    Error: [$parse:syntax] Syntax Error: Token '.0' is an unexpected token at column 8 of the expression [markers.0] starting at [.0].
http://errors.angularjs.org/1.4.8/$parse/syntax?p0=.0&p1=is%20an%20unexpected%20token&p2=8&p3=markers.0&p4=.0
    at angular.js:68
    at Object.AST.throwError (angular.js:13100)
    at Object.AST.ast (angular.js:12870)
    at Object.ASTCompiler.compile (angular.js:13319)
    at Parser.parse (angular.js:14189)
    at $parse (angular.js:14291)
    at Scope.$watch (angular.js:15482)
    at createMarker (angular-leaflet-directive.js:1016)
    at Object.fn (angular-leaflet-directive.js:795)
    at Scope.$digest (angular.js:15896)

如果有帮助,这里有一些代码。

控制器.js:

$scope.location = {lat: 8.812354, lng: -11.887342};

            $scope.center = {
                lat: 8.812354,
                lng: -11.887342,
                zoom: 8
            };

            $scope.markers = [];
            $scope.markers.push({
                lat: 8.812354,
                lng: -11.667342,
                message: "hehe"
            });

    //This one is added to the array, but doesn't show up in the map
            $scope.markers.push({
                lat: 7.812354,
                lng: -10.667342,
                message: "WOOP"
            });

$scope.$on("leafletDirectiveMap.click", function (event, args) {

            var leafEvent = args.leafletEvent;
            console.log('Ctrl3 adding marker at lat=' + leafEvent.latlng.lat + ', lng=' + leafEvent.latlng.lng);
            $scope.location.lng = leafEvent.latlng.lng;
            $scope.location.lat = leafEvent.latlng.lat;

            $scope.markers.push({
                lat: leafEvent.latlng.lat,
                lng: leafEvent.latlng.lng,
                message: "My Added Marker"
            });
        });

HTML:

<leaflet class="col-md-offset-4 map" defaults="defaults" markers="markers" center="center" layers="layers"></leaflet>
4

1 回答 1

1

请注意,$scope.markers应该是一个对象,而不是一个数组。该指令试图枚举markers对象的属性,但在数组具有的数字属性上失败。

更改您的$scope.markersto{}并在某个键处向对象添加新标记,而不是将push其添加到数组中,例如:

$scope.markers = {};

// ...

var idx = 0;
$scope.$on("leafletDirectiveMap.click", function (event, args) {
    idx += 1;

    $scope.markers['marker' + idx] = {
        // ...
    };
});
于 2015-11-24T21:43:44.723 回答