Angular 令牌错误

Angular Token Error

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

    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)

下面是一些代码,如果有帮助的话。

Controller.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>

注意$scope.markerssupposed to be an object,不是数组。该指令试图枚举 markers 对象的属性,但在数组具有的数字属性上失败。

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

$scope.markers = {};

// ...

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

    $scope.markers['marker' + idx] = {
        // ...
    };
});