我应该如何管理AngularJS中的依赖项?

时间:2022-06-24 07:15:30

I've got a question about dependencies management. Have a look at the example:

我有一个关于依赖关系管理的问题。看看这个例子:

MyApp.controller("MyController", ["$scope", "$document", "$timeout", "SomeService",
function(scope, doc, timeout, service){

   /*Some Code here*/

}]);

As you can see, the dependencies list + function statement need a lot of writing and it looks a little bit messy. I know, that I could put all this 'array' in some variable and then inject it inside the controller definition but I don't think, that it's the best idea. How can I make controller definition with dependencies more clear? Should I deal with RequireJS or something like that?

如您所见,依赖列表+函数语句需要大量编写,看起来有点乱。我知道,我可以把所有这个'数组'放在一个变量中,然后将它注入控制器定义中,但我不认为,这是最好的主意。如何使依赖关系的控制器定义更清晰?我应该处理RequireJS或类似的东西吗?

Thanks!

2 个解决方案

#1


0  

Have a look here best guidelines ever for angularJS

看看这里有关angularJS的最佳指导

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

Cheers

#2


0  

RequireJS comes in handy if you have a ton of routes and pages and you don't want to compress all of your controllers, services, directives, filters etc. together and want to break out your dependencies by path.

如果您有大量的路由和页面,并且您不想将所有控制器,服务,指令,过滤器等压缩在一起并希望按路径分解依赖项,则RequireJS会派上用场。

The tradeoff here is that this approach is worth the effort only if the level of complexity and size of payload is very high e.g. If you have ten routes, two of which have 100KB of JS, with the rest have 30KB of corresponding JS, and you don't want to serve the 130KB on pageload, considering the the user won't be accessing the 100KB route all the time.

这里的权衡是,只有当有效载荷的复杂程度和大小非常高时,这种方法才值得努力。如果你有10个路由,其中​​两个有100KB的JS,其余的有30KB的相应JS,你不想在页面加载上提供130KB,考虑到用户不会访问100KB路由全部时间。

If it is a straightforward application, then RequireJS is not required in most cases (NotRequiredJS. Sorry). It's relatively complicated to implement. But if you're looking at lots of scalability (ideally if you have lots of time, you SHOULD), then you might want to consider RequireJS.

如果它是一个简单的应用程序,则在大多数情况下不需要RequireJS(NotRequiredJS。抱歉)。实施起来相对复杂。但是,如果你正在寻找大量的可扩展性(理想情况下,如果你有很多时间,你应该),那么你可能想要考虑RequireJS。

#1


0  

Have a look here best guidelines ever for angularJS

看看这里有关angularJS的最佳指导

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

Cheers

#2


0  

RequireJS comes in handy if you have a ton of routes and pages and you don't want to compress all of your controllers, services, directives, filters etc. together and want to break out your dependencies by path.

如果您有大量的路由和页面,并且您不想将所有控制器,服务,指令,过滤器等压缩在一起并希望按路径分解依赖项,则RequireJS会派上用场。

The tradeoff here is that this approach is worth the effort only if the level of complexity and size of payload is very high e.g. If you have ten routes, two of which have 100KB of JS, with the rest have 30KB of corresponding JS, and you don't want to serve the 130KB on pageload, considering the the user won't be accessing the 100KB route all the time.

这里的权衡是,只有当有效载荷的复杂程度和大小非常高时,这种方法才值得努力。如果你有10个路由,其中​​两个有100KB的JS,其余的有30KB的相应JS,你不想在页面加载上提供130KB,考虑到用户不会访问100KB路由全部时间。

If it is a straightforward application, then RequireJS is not required in most cases (NotRequiredJS. Sorry). It's relatively complicated to implement. But if you're looking at lots of scalability (ideally if you have lots of time, you SHOULD), then you might want to consider RequireJS.

如果它是一个简单的应用程序,则在大多数情况下不需要RequireJS(NotRequiredJS。抱歉)。实施起来相对复杂。但是,如果你正在寻找大量的可扩展性(理想情况下,如果你有很多时间,你应该),那么你可能想要考虑RequireJS。