$ compile未在角度控制器中定义

时间:2016-06-11 15:26:16

标签: javascript angularjs google-maps ecmascript-6

我正在尝试向Google地图标记添加一个按钮,当我尝试打开标记时出现错误。

  

未捕获的ReferenceError:$ compile未定义

我设置标记的功能:

setMarker(placeId) {
    var _map = this.map;
    var _this = this;

    var request = {
        placeId: placeId
    };

    var service = new google.maps.places.PlacesService(_map);
    var infowindow = new google.maps.InfoWindow();

    service.getDetails(request, function (place, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            // If the request succeeds, draw the place location on the map
            // as a marker, and register an event to handle a click on the marker.
            var marker = new google.maps.Marker({
                map: _map,
                position: place.geometry.location
            });

            google.maps.event.addListener(marker, 'click', function() {
                var HTML = '';
                HTML +=
                    '<div ng-controller="DashboardController">' +
                    '   <strong>' + place.name + '</strong><br>' +
                        place.formatted_address + '<br><br>' +
                        '<button class="btn btn-primary" ng-click="addPlaceToItinerary(' + place.place_id + ')">Add to itinerary</button>' +
                    '</div>';

                var compiled = $compile(HTML)($scope);

                infowindow.setContent({
                    content: compiled[0]
                });

                google.maps.event.addDomListener(window, 'load', initialize);

                infowindow.open(_map, this);
            });

            _map.panTo(place.geometry.location);
        }
    });
}

$ compile使用$ inject注入控制器。

LE:

DashboardController.$inject = [
    '$scope',
    '$compile',
    'authService'
];

LE2:

这就是我注入所有东西的方式。

class DashboardController extends BaseInjectable {
    constructor(...injectables) {
        super(injectables, DashboardController.$inject);
    }
}

BaseInjectable看起来像这样:

export default class BaseInjectable {
    constructor(instances, names) {
        instances.forEach((instance, index) => {
            this[names[index]] = instance;
        });
    }
}

0 个答案:

没有答案