简体   繁体   English

使用Requirejs在底部加载JavaScript

[英]Loading javascripts at the bottom using Requirejs

I've been trying to implement this POST . 我一直在尝试实现此POST But i have seen that using RequireJs either in head or in the bottom, it always loads the dependent javascript files in the head. 但是我已经看到,无论是在头部还是在底部使用RequireJs,它总是将依赖的javascript文件加载到头部。 Is there any way i can force RequireJs to always load the dependent scripts at the bottom?? 有什么办法可以迫使RequireJs始终在底部加载依赖脚本?

The problem i am trying to solve is: I want to load specific controller depending on the route. 我要解决的问题是:我想根据路线加载特定的控制器。 But i have been defining ng-controller="MyCtrl" attribute in the views. 但是我一直在视图中定义ng-controller =“ MyCtrl”属性。 So when the all the dependent scripts and specific controller loads its not able to find the ng-controller attribute and gives me the following error: 因此,当所有依赖脚本和特定控制器加载时,它无法找到ng-controller属性,并给我以下错误:

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

Is this the issue or am i missing something else here? 这是问题还是我在这里错过了其他东西?

Most asynchronous loaders would load the scripts in head. 大多数异步加载器会在头中加载脚本。 There is practically no advantage in trying to load anything in the bottom at this point now that the DOM has already been parsed. 既然已经解析了DOM,那么此时尝试在底部加载任何内容几乎没有任何好处。 So whether you add require.js in the head or bottom it should not matter. 因此,无论在顶部还是底部添加require.js都没有关系。

About the error you are getting:- 关于您得到的错误:-

Are you using something like resolve: resolveController('/app/controllers/customersController.js') and included routeResolver.js 您是否使用类似resolve的东西:resolveController('/ app / controllers / customersController.js')和包括的routeResolver.js

If you don't want to load the script in header you could "require" the "require" function and then require the module whatever you want to use: 如果您不想在头文件中加载脚本,则可以“需要”“需要”功能,然后根据需要使用该模块:

define(['require'], function(require){

    ...

    var ctrl = require('path/to/your/controller');

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM