简体   繁体   English

angularjs从不同的模块路由到控制器

[英]angularjs route to controllers from different modules

At the moment we are in the process of creating a new web application infrastructure. 目前,我们正在创建一个新的Web应用程序基础结构。

We initially load a dashboard which is esentially the top bar displaying the logged in user and the set of menus along with it. 首先,我们加载一个仪表板,该仪表板本质上是显示已登录用户和菜单集的顶部栏。 Clicking on each menu would load a screen (mostly crud screens) in the main section. 单击每个菜单会在主要部分加载一个屏幕(主要是粗略屏幕)。 We areplanning to put each of the crud screens and their components (services, controllers and such) in a seperate module which will encapsulate all the screens from each other, so for example if there is 78 screens there will be 78 seperate modules for each screen. 我们计划将每个原始屏幕及其组件(服务,控制器等)放在一个单独的模块中,该模块将相互封装所有屏幕,例如,如果有78个屏幕,则每个屏幕将有78个单独的模块。 We are also using planing on using Requirejs to load these dependencies dynamically. 我们还计划使用Requirejs动态加载这些依赖项。

The problem however occurs that we need to link the menu with each of the modules for each screen. 但是,出现了问题,我们需要将菜单与每个屏幕的每个模块链接起来。 Typically in a single module based app it would be done like this. 通常,在基于单个模块的应用程序中,将这样做。

config(function($routeProvider, $locationProvider) {
  $routeProvider
   .when('/Book/load', {
    templateUrl: 'book.html',
    controller: 'BookController'
  })
  .when('/Screen/load', {
    templateUrl: 'chapter.html',
    controller: 'ChapterController'
  });

Where the BookController and ChapterController will be in the SAME module. BookController和ChapterController将在SAME模块中的位置。

However in our case the BookController will be in a BookModule for the book screen and the same applies for the ChapterController. 但是,在我们的示例中,BookController将位于用于书本屏幕的BookModule中,并且对于ChapterController也是如此。 And the routes would be in the initial main module for example AppModule which loads the dashboard initially during startup then. 路由将位于初始主模块(例如AppModule)中,该模块最初在启动期间加载仪表板。

How would we say for example link the AppModule and the routes with each module for each screen (for example in this case BookController and ChapterController) keeping in mind that we need to load the modules dynamically when NEEDED using requirejs. 例如,我们要怎么说呢?请牢记在使用requirejs进行NEEDED时,我们需要动态加载模块,从而将AppModule和路由与每个屏幕的每个模块链接起来(例如,在本例中为BookController和ChapterController)。

(PS : We are essentially segmenting our application based on feature where feature in our system usually equals screen) (PS:我们实质上是根据功能来划分应用程序,而系统中的功能通常等于屏幕)

Also any suggestions on any other way we could best structure our app including an answer to the above problem would be very much appreciated. 同样,我们将以任何其他方式最好地构建应用程序的建议,包括对上述问题的解答。

Regards, Milinda 问候,米林达

Why do you have the route configuration in the initial main module? 为什么在初始主模块中具有路由配置? This creates unnecessary coupling between your modules (ie. your initial module has to know all the possible routes of every module), sort of defeating the purpose of moving your code into modules in the first place. 这在您的模块之间创建了不必要的耦合(即,您的初始模块必须知道每个模块的所有可能路径),从而破坏了首先将代码移入模块的目的。

Each of your modules can have their own route configuration, which will take effect when the modules are loaded. 每个模块都可以具有自己的路由配置,该配置将在加载模块时生效。 (A consistent naming convention can help avoiding clashes between routes of unrelates modules) (一致的命名约定可以帮助避免在不相关模块的路由之间发生冲突)

While configuring the routeProvider, you can use lazy loading mechanisms using the resolve attribute of routes: resources referenced here will be resolved before the routeChange event happens, which enables you to wait for any promise resolution or a requireJS loading of a file. 在配置routeProvider时,可以使用路由的resolve属性来使用延迟加载机制:这里引用的资源将在routeChange事件发生之前得到解析,这使您可以等待任何承诺解析或文件的requireJS加载。 This blogpost might help in that regard. 该博客文章可能会在这方面有所帮助。

As of this moment, there is no mechanism as far as I am aware for dynamically loading modules at runtime and then incorporating them in an AngularJS app. 到目前为止,据我所知,还没有一种机制可以在运行时动态加载模块,然后将其合并到AngularJS应用中。 You can breakdown your app into 78 individual modules loaded via requirejs, but you will still need a single primary module which has all those other 78 as dependencies. 您可以将应用细分为通过requirejs加载的78个单独的模块,但是您仍然需要一个具有所有其他78个依赖关系的单个主模块。 It is this primary module which you will then configure all the routes. 然后是您将配置所有路由的主要模块。

There is work going on into a new AngularJS router which borrows from other more flexible routers (ie ui-router, etc) which will allow exactly the sort of dynamic loading of modules you are speaking about but as far as I know it won't be available until AngularJS 1.4. 有一个新的AngularJS路由器正在进行工作,该路由器借鉴了其他更灵活的路由器(即ui-router等),这将完全允许您正在谈论的模块的动态加载,但据我所知在AngularJS 1.4之前可用。

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

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