热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UsingOpenLayerswithRequireJSandAngularJS

ImtryingtogetanapprunningthatusesbothAngularJSandRequireJS.Imhavingproblemsgetting

I'm trying to get an app running that uses both AngularJS and RequireJS. I'm having problems getting my OpenLayers lib to work in this setup.

我正在尝试运行使用AngularJS和RequireJS的应用程序。我在让我的OpenLayers lib在此设置中工作时遇到问题。

I set the main AMD-modules in the main.js:

我在main.js中设置了主要的AMD模块:

require.config(
    {
        baseUrl: 'scripts',
        paths: {
            // Vendor modules
            angular: 'vendor/angular/angular',
            openLayers: 'vendor/openlayers-debug',
            other modules.....
        },
        shim: {
            angular: {
                exports: 'angular'
            },
            openLayers: {
                exports: 'OpenLayers'
            },
            other modules....
        }
    }
);

require(['openLayers',
        'angular',
        'app',
        'controllers/olMapController',
        'directives/olMap',
        other modules...
    ], function(OpenLayers) {
        return OpenLayers;
    }
);

Then in the angular controller I use for the initialisation of OpenLayers, I try to indicate that openlayers-debug.js is a dependency:

然后在我用于初始化OpenLayers的角度控制器中,我试着指出openlayers-debug.js是一个依赖:

define(['openLayers'],
    function(OpenLayers) {
        controllers.controller('olMapController', ['$scope', function(scope) {
            console.log('Loaded olMapController. OpenLayers version: ' + OpenLayers.VERSION_NUMBER);
        }]);
    }
);

Well, this doesn't work. SOMETIMES the olMapController function is executed, but mostly not. The console then just displays an error stating:

嗯,这不起作用。有时执行olMapController函数,但大部分都没有。然后控制台只显示一个错误说明:

Error: [ng:areq] Argument 'olMapController' is not a function, got undefined

错误:[ng:areq]参数'olMapController'不是函数,未定义

So, I think OpenLayers hasn't finished loading yet, but somehow require thinks it has and continues loading code that depends on OpenLayers, in this case the olMapController. It then can't find its dependency, whereupon Angular returns this error message. Or something like that...? Sometimes something happens that makes OpenLayers load fast enought for it to be present when it is loaded as a dependency. What that is, I can't tell.

所以,我认为OpenLayers尚未完成加载,但不知何故需要认为它已经并继续加载依赖于OpenLayers的代码,在本例中是olMapController。然后它无法找到它的依赖关系,于是Angular返回此错误消息。或类似的东西...?有时发生的事情会使OpenLayers加载得很快,因为当它作为依赖项加载时它会存在。那是什么,我说不出来。

I left out other libraries and modules require and define to keep the code readable. I hope the example is still understandable.

我遗漏了其他库和模块需要并定义以保持代码可读。我希望这个例子仍然可以理解。

Any ideas on what I could do to get openlayers to load well? The error message disappears when I leave the ['openLayers'] dependency out of olMapController.

有什么想法可以让openlayers加载好吗?当我将['openLayers']依赖项从olMapController中删除时,错误消息消失。

Thanks in advance for any help.

在此先感谢您的帮助。

Best regards, Martijn Senden

最好的问候,Martijn Senden

2 个解决方案

#1


3  

Sorry this answer will only contain text as your code is too big for a small example.

对不起,这个答案只包含文字,因为你的代码太大而不适用于一个小例子。

I would suggest to write a directive that utilizes head.js to load libraries you need at a specific context. One could also think of a directive that initializes openLayers this way.

我建议编写一个使用head.js来加载特定上下文所需库的指令。人们还可以想到一个以这种方式初始化openLayers的指令。

I guess your error is a timing problem, because the require.js and angular.js module loading gets out of sync - more precisely there seems to be no sync at all between them.

我猜您的错误是一个计时问题,因为require.js和angular.js模块加载不同步 - 更确切地说,它们之间似乎根本没有同步。

update
To repeat my comment which lastly helped to lead the OP in the right direction:

更新重复我的评论,这最终有助于引导OP朝着正确的方向发展:

You have to decide which framework gives the tone. If you go with requireJS, then require everything and bootstrap angular manually. (Remove ng-app="" from index.html) and do `angular.bootstrap()´ when requirejs has completed. So the problem most likely is, that angular starts too early.

你必须决定哪个框架给出基调。如果您使用requireJS,则需要手动执行所有操作和引导角度。 (从index.html中删除ng-app =“”)并在requirejs完成后执行`angular.bootstrap()'。所以最有可能的问题是角度开始得太早。

#2


5  

Well, just for reference my final solution. The comment by angabriel set me off in the right direction.

好吧,仅供参考我的最终解决方案。安格布里尔的评论让我朝着正确的方向前进。

Like i said, I'm using the domReady module provided by RequireJS to bootstrap Angular. This is still being called to early, because OpenLayers isn't loaded yet when angular starts. RequireJS also provides a callback property in require.config. This is a function that is triggered when all the dependencies are loaded. So I used that function to require the angular bootstrap module. My config now looks like this:

就像我说的,我正在使用RequireJS提供的domReady模块来引导Angular。这仍然被提前调用,因为当角度开始时OpenLayers尚未加载。 RequireJS还在require.config中提供了一个回调属性。这是在加载所有依赖项时触发的函数。所以我使用该函数来要求角度引导模块。我的配置现在看起来像这样:

require.config(
    {
        baseUrl: 'scripts',
        paths: {
            // Vendor modules
            angular: 'vendor/angular/angular',
            domReady: 'vendor/domReady',
            openLayers: 'vendor/openlayers-debug',
            etcetera....

        },
        shim: {
            angular: {
                deps: ['jquery'],
                exports: 'angular'
            },
            openLayers: {
                exports: 'OpenLayers'
            },
        },
        deps: ['angular',
            'openLayers',
            'app',
            'domReady',
            'controllers/rootController',
            'controllers/olMapController',
            'directives/olMap'
        ],
        callback: function() {
            require(['bootstrap']);
        }
    }
);

And the bootstrap module looks like this:

而bootstrap模块看起来像这样:

define(['angular', 'domReady', 'app'], function(angular, domReady) {
    domReady(function() {
        angular.bootstrap(document, ['GRVP']);
    });
});

Thanks for the help. @angabriel: I upvoted your comment. It's not possible to accept a comment as an answer, is it? Your initial answer wasn't really the answer to my question, but the comment helped a lot...

谢谢您的帮助。 @angabriel:我赞成你的评论。接受评论作为答案是不可能的,是吗?你最初的答案并不是我问题的答案,但评论帮了很多...

Regards, Martijn


推荐阅读
author-avatar
不要破网名_329
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有