Angular js何时创建单独的控制器

Angular js when to create separate controller

我正在实现(添加、更新、删除和获取、过滤)功能。

目前添加视图用于添加、更新和删除。 (控制器 1) 获取视图用于获取和筛选。 (控制器 2)

为单独添加和获取创建路由。所以每个 2 个控制器。 现在我必须为两个控制器中使用的通用配置数据调用服务。

我更喜欢以下哪个设计。

设计 1

将两个控制器合并为一个控制器,将所有代码放入其中。(包括配置数据)

设计 2

一个 Get 控制器,filter 添加、更新和删除的第二个控制器 配置数据的通用工厂。

迷茫,从各方面来说我更喜欢哪种设计。 请提出建议。

提前致谢。

"skinny controllers"

是个好习惯

编辑 -

In AngularJS, controllers can sometimes become monolithic structures of mixed business and view logic, as it’s all too easy to house anything you need for the view inside the controller. It’s convenient, and it just plain works… until your application grows in complexity or needs unit tests.

So why do we want skinny controllers, anyway? In short, separation of concerns. Ideally, everything from services, to controllers, to directives, and more should be skinny, and achieving this is very possible in AngularJS. Each part should have a single responsibility and the controller’s responsibility should be to communicate between services and the view; i.e. its main concern should be view-model logic.

Aim to make your controllers skinny, as well as the rest of your application, by separating out view logic and business logic into controllers and services, respectively, and by taking advantage of routes and resolves.

您可以在 this article 中详细阅读并查看代码示例