如何从角度以外的javascript函数获取角度js创建的对象实例

时间:2016-06-26 22:08:03

标签: javascript angularjs google-maps

我有角度app写得很好,但我必须访问由angular创建的对象的实例,下面是角度代码;

在其中,有[g.mapInstance = i = ....]行。我必须使用纯角度以外的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

1 个答案:

答案 0 :(得分:0)

我明白了。希望它也有助于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);