简体   繁体   English

分离角路由,控制器和服务

[英]Separating Angular Routing, Controllers, Services

You'll have to forgive the lack of code example on this one; 您将不得不原谅缺少此示例的代码示例。 however, I'm hoping this question is straight forward enough that it will not require one. 但是,我希望这个问题简单明了,以至于不需要。

I'm new to working with Angular. 我是刚接触Angular的人。 The project I'm working on isn't terribly big. 我正在从事的项目并不十分庞大。 Would it be practical to have controllers in one file, routing in another file and services in the last file? 在一个文件中包含控制器,在另一个文件中路由以及在最后一个文件中提供服务是否可行? If so, how do I manage the dependencies so that I can reference my controllers from the routing file? 如果是这样,如何管理依赖性,以便可以从路由文件中引用控制器?

Note: This only answers the file/folder structure. 注意:这仅回答文件/文件夹结构。 Ideally this would be compiled into a single file that is referenced into your app. 理想情况下,它将被编译成引用到您的应用程序中的单个文件。 Or compiled into feature files which are included separately. 或编译成单独包含的功能文件。


Based on the Guide from John Papa: 根据约翰·帕帕(John Papa)的指南:

https://github.com/johnpapa/angular-styleguide#locate https://github.com/johnpapa/angular-styleguide#locate

And Todd Motto 和托德·座右铭

http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/ http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/

https://github.com/toddmotto/angularjs-styleguide https://github.com/toddmotto/angularjs-styleguide

The two best approaches IMO are, for smaller projects, by type: 对于较小的项目,IMO的两种最佳方法是按类型:

|-- app.js
|-- controllers/
|   |-- MainCtrl.js
|   |-- AnotherCtrl.js
|-- filters/
|   |-- MainFilter.js
|   |-- AnotherFilter.js
|-- services/
|   |-- MainService.js
|   |-- AnotherService.js
|-- directives/
|   |-- MainDirective.js
|   |-- AnotherDirective.js

Or for larger project, feature-driven: 对于大型项目,功能驱动:

|-- app.js
|-- dashboard/
|   |-- DashboardService.js
|   |-- DashboardCtrl.js
|-- login/
|   |-- LoginService.js
|   |-- LoginCtrl.js
|-- inbox/
|   |-- InboxService.js
|   |-- InboxCtrl.js

You want to keep the folder structure relatively flat so you don't have to spend ages navigating folders to find the files you want. 您想要使文件夹结构保持相对平坦,这样就不必花很多时间浏览文件夹来查找所需的文件。

Of course you can. 当然可以。 This is a normal code organization behavior you follow with angular. 这是您使用angular遵循的正常代码组织行为。 as you are new, I have a sample angular app that are very simple. 当您是新手时,我有一个非常简单的示例角度应用程序。 I'm using this app with my students to explain Single page application using angular and node js. 我与学生一起使用此应用程序,以解释使用angular和node js的单页应用程序。

you can find a link to what you ask as a sample here https://github.com/mustafamg/CourseManagementSystem/tree/master/public/app 您可以在此处找到指向您所要求的示例的链接https://github.com/mustafamg/CourseManagementSystem/tree/master/public/app

In this link you can find routing an application preparation in app.js, while controllers in controllers folder. 在此链接中,您可以在app.js中找到路由应用程序准备,而在controllers文件夹中找到控制器。

You simply reference app.js in your html file followed by controller.js 您只需在html文件中引用app.js,然后再引用controller.js

<script src="app.js"></script>
<script src="controller.js"></script>

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

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