简体   繁体   English

将单个Angular Controller拆分为多个JS文件

[英]Split a single Angular Controller into multiple JS files

am trying to create an application with Angularjs for our financial client. 我正在尝试使用Angularjs为我们的金融客户创建一个应用程序。 They need the whole application in one single page. 他们需要整个应用程序在一个页面中。 I am convincing them to use tabs at least. 我说服他们至少使用标签。 But the actual problem is the application has almost 2000 fields and half of them are calculated based on the input and some numbers from the database. 但实际问题是应用程序有近2000个字段,其中一半是根据输入和数据库中的一些数字计算的。

I have segregated the application using few controllers. 我使用很少的控制器隔离了应用程序。 But still each controller will be humongous given all the calculations. 但是在给出所有计算的情况下,每个控制器仍然是巨大的。

  1. Is there a way to split a single Controller into multiple JS files? 有没有办法将单个Controller拆分成多个JS文件?
  2. We have developed similar application for the same client using KnockoutJS. 我们使用KnockoutJS为同一客户开发了类似的应用程序。 Which was fairly easy with observables and Computed features from KO. 使用KO的可观察量和计算特征相当容易。 Or is it a better idea to stay with KO ? 或者与KO呆在一起是否更好? Imagining doing all the calculation across Controllers seems to be very difficult. 想象在控制器之间进行所有计算似乎非常困难。

Please suggest. 请建议。

Angular isn't bad by design, but it is prone to be misused. Angular在设计上并不差,但它很容易被误用。 The fact that Angular doesn't suggest anything for models or inheritance (you're on your own with JS prototypes, and POJO, and extend or copy for mixins) doesn't help at all. Angular没有为模型或继承提出任何建议(你自己使用JS原型,POJO,以及为mixin extendcopy )这一事实根本没有帮助。

If you have the background with modern MVC frameworks in OOP languages, you're probably familiar with 'thin controllers, fat models' conception. 如果您拥有OOP语言中现代MVC框架的背景,那么您可能熟悉“瘦控制器,胖模型”的概念。 In Angular case it is 'thin controllers, fat services' (it isn't MVC but MVW framework after all). 在Angular案例中,它是'瘦控制器,胖服务'(毕竟它不是MVC,而是MVW框架)。

If you're about to detach some functions into separate object (file?), then it is ready-to-go service (most likely factory ). 如果您要将某些功能分离到单独的对象(文件?)中,那么它就是准备就绪的服务 (很可能是factory )。 Think of Angular services as of usual objects (models, utility classes, asynchronous services, whatever) that fit DI pattern. 将Angular服务视为适合DI模式的常规对象(模型,实用程序类,异步服务等)。

It is quite amusing to see how Angular good practices developed in the last couple of years. 看看Angular在过去几年中的良好实践是如何发展的,这是非常有趣的。 One of them is using the controller for the sole thing where they shine - getting the data from services and putting it into scope, and using $scope.$... functions, it helps to avoid scope soup . 其中一个是将控制器用于它们闪耀的唯一东西 - 从服务获取数据并将其放入范围,并使用$scope.$...函数,它有助于避免范围汤

Another recent conception (influenced by web components) I'm fond of is replacing controllers ( ng-controller is itself a directive, which offers you nothing but controller, hence the name) with directives. 我最喜欢的另一个概念 (受Web组件影响)是替换控制器( ng-controller本身就是一个指令,它只提供控制器,因此名称),带有指令。 The latter may provide higher level of encapsulation (via isolate scope) and controller interoperation (with require ). 后者可以提供更高级别的封装(通过隔离范围)和控制器互操作(具有require )。

It have taken some time for me to figure it out, but if you have to struggle with Angular, you're probably doing something wrong. 我花了一些时间来弄明白,但如果你不得不与Angular斗争,你可能做错了什么。

The short answer is no . 简短的回答是否定的 Your controller must be defined as a single function. 您的控制器必须定义为单个功能。

However you should break the logic in the controller into separate services. 但是,您应该将控制器中的逻辑分解为单独的服务。 In fact the controller should have very little logic in it. 事实上,控制器应该具有非常少的逻辑。 All data you need to share with other controllers and directive should be stored within the services you create. 您需要与其他控制器和指令共享的所有数据都应存储在您创建的服务中。 Separate directives and controllers would use the services to update the get the values they need. 单独的指令和控制器将使用服务来更新获取所需的值。

Also note that if you have anything in the controller that is manipulating the DOM all of that logic should be in a directive. 另请注意,如果控制器中有任何操作DOM的内容,则所有逻辑都应该在指令中。

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

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