如何获得由Angular外部JavaScript函数创建的对象的实例



我编写了Angular应用程序,但是我必须访问由Angular创建的对象的实例,下面是Angular Code;

内部有[G.Mapinstance = i = ....]行。我必须在Angular外部使用纯JavaScript访问地图实例。我希望我能清楚我的问题。非常感谢...

    a.module("myapp.maps", []).value("mapsConfig", {}).factory("mapsApi", ["$window", "$q", "mapsConfig", function(a, b, c) {
    return b(function(b) {
        var d = c.apiKey || "",
            e = c.libraries || [],
            f = e.join(",");
        a.__handleApiReady = function() {
            b(google.maps)
        };
        $script("//maps.googleapis.com/maps/api/js?sensor=false" + (d ? "&key=" + d : "") + (f ? "&libraries=" + f : "") + "&callback=__handleApiReady")
    })
}]).directive("maps", ["mapsApi", function(c) {
    return {
        restrict: "A",
        replace: true,
        transclude: true,
        scope: {
            center: "=center",
            zoom: "=zoom",
            dragging: "=dragging",
            control: "=",
            options: "=options",
            events: "=events",
            styles: "=styles",
            bounds: "=bounds",
            geocode: "="
        },
        template: '<div><div class="map-container"></div><div ng-transclude style="display: none"></div></div>',
        controller: ["$scope", "$element", "$attrs", function(d, e, f) {
            var g = this,
                h = d.events,
                i;
            c.then(function(c) {
                var j = {
                        zoom: 14,
                        mapTypeId: c.MapTypeId.ROADMAP,
                        mapTypeIds: [c.MapTypeId.ROADMAP, c.MapTypeId.HYBRID],
                        mapTypeControlOptions: {
                            style: c.MapTypeControlStyle.HORIZONTAL_BAR,
                            position: c.ControlPosition.TOP_RIGHT,
                            mapTypeIds: [c.MapTypeId.ROADMAP, c.MapTypeId.SATELLITE]
                        },
                        zoomControl: true,
                        zoomControlOptions: {
                            style: c.ZoomControlStyle.SMALL
                        },
                        streetViewControl: false,
                        scrollwheel: false,
                        panControl: true,
                        panControlOptions: {
                            position: c.ControlPosition.TOP_LEFT
                        },
                        scaleControl: false,
                        scaleControlOptions: {
                            position: c.ControlPosition.BOTTOM
                        },
                        disableDoubleClickZoom: true
                    },
                    k = d.options;
                g.mapInstance = i = new c.Map(e.find("div")[0], a.extend({}, j, k));
                d.$watch("bounds", function(a) {
                    if (a && a.length === 4) {
                        i.fitBounds(new c.LatLngBounds(new c.LatLng(a[2], a[3]), new c.LatLng(a[0], a[1])))
                    }
                });
                d.$watch("geocode", function(a) {
                    if (a) {
                        var b = new c.Geocoder,
                            d;
                        b.geocode({
                            address: a
                        }, function(a, b) {
                            if (b == "OK" && a.length > 0) {
                                d = a[0].geometry;
                                i.setCenter(d.location);
                                if (d.hasOwnProperty("bounds")) {
                                    i.fitBounds(d.bounds)
                                }
                            }
                        })
                    }
                });
                if (h) {
                    a.forEach(h, function(a, b) {
                        c.event.addListener(i, b, function() {
                            a.apply(i, arguments)
                        })
                    })
                }
                a.forEach(f.$attr, function(e, g) {
                    if (g.indexOf("options") === 0 && g.length > 7) {
                        f.$observe(g, function(b) {
                            var c = {};
                            c[g.replace("options", "")] = b;
                            i.setOptions(a.extend({}, j, c))
                        })
                    }
                    if (g.indexOf("events") === 0 && g.length > 6) {
                        var h = b(g.replace("events", ""));
                        c.event.addListener(i, h.replace("-", "_"), function() {
                            d.$parent.$eval(f[g])
                        })
                    }
                })
            })
        }]
    }...... goes on

我得到了它。希望它也对Soneone有所帮助..感谢Morningdew给出了线索。以下是解决方案

var s = angular.element(document.querySelector(".map-container")).scope(); 
var b = {}; b.latLng = {}; b.latLng.lat = function(){ return 41.005329; } ; 
b.latLng.lng = function(){return 28.891567; };
s.events.click(b);

最新更新