首页 > 解决方案 > 为 Angular 项目包装 google-closure-library 变量

问题描述

所以,从这个问题开始

我现在在 bower 和 npm 中都有我的模块,我可以将它很好地导入我的项目并在 app.module 中声明它,甚至尝试在我的控制器中使用它:

索引.html:

<script src="bower_components/google-closure-library/closure/goog/base.js"></script>

app.module.js:

angular
        .module('drugQualityDataManagerApp', [
            'ngStorage',
            'ngResource',
            'ngCookies',
            'ngAria',
            'web-gl-earth2',

上传数据.controller.js:

$scope.initializeMapWebGL = function(){
                var earth = new webGlEarth.map('earth_div');
                webGlEarth.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(earth);

                var marker = webGlEarth.marker([51.5, -0.09]).addTo(earth);
                marker.bindPopup("<b>Hello world!</b><br>I am a popup.<br /><span style='font-size:10px;color:#999'>Tip: Another popup is hidden in Cairo..</span>", {maxWidth: 150, closeButton: true}).openPopup();

                var marker2 = webGlEarth.marker([30.058056, 31.228889]).addTo(earth);
                marker2.bindPopup("<b>Cairo</b><br>Yay, you found me!", {maxWidth: 120, closeButton: false});

                var markerCustom = webGlEarth.marker([50, -9], '/img/logo-webglearth-white-100.png', 100, 24).addTo(earth);

                earth.setView([51.505, 0], 6);
            };

上传数据.html:

<style>
    html, body{padding: 0; margin: 0; background-color: black;}
    #earth_div{top: 0; right: 0; bottom: 0; left: 0; position: absolute !important;}
</style>
<title>WebGL Earth API: Markers</title>
<body onload="initialize()">
<div id="earth_div"></div>
</body>

但是,当然,这不会那么直截了当,问题来自 webgearth2 的依赖项,这个应用程序强烈依赖于 google-closure-library,而 angular 不喜欢这样,我尝试过包装main.js 文件,如:

(function () {
    'use strict';
    angular.module('web-gl-earth2', []).provider('webGlEarth');

我将 google-closure-library 添加到我的 bower_components 和 app.module.js 中,但我仍然感到害怕:

Uncaught ReferenceError: goog is not defined

所以,我留下了一个问题:是否有一个自动包装器可以帮助我将这个模块集成到 Angular 中,还是我注定要重新编写整个源代码以使其“有角度的可爱”?

标签: javascriptangularjsgoogle-closure-library

解决方案


推荐阅读