本文翻译自:Does it make sense to use Require.js with Angular.js? [closed]
I'm a newbie to Angular.js and trying to understand how it's different from Backbone.js... We used to manage our packages dependencies with Require.js while using Backbone. 我是Angular.js的新手,并试图理解它与Backbone.js的不同之处......我们曾经使用Backbone来管理我们的包依赖关系和Require.js。 Does it make sense to do the same with Angular.js? 使用Angular.js做同样的事情是否有意义?
参考:https://stackoom.com/question/qZNz/将Require-js与Angular-js一起使用是否有意义-关闭
Yes it makes sense to use angular.js
along with require.js
wherein you can use require.js
for modularizing components. 是的,使用angular.js
和require.js
是有意义的,其中您可以使用require.js
来模块化组件。
I can point you to a seed project which uses both angular.js and require.js
. 我可以指出你使用both angular.js and require.js
的种子项目 。 Hope that helps! 希望有所帮助!
This I believe is a subjective question, so I will provide my subjective opinion. 我认为这是一个主观问题,所以我将提出我的主观意见。
Angular has a modularization mechanism built in. When you create your app, the first thing you would do is Angular内置了模块化机制。当您创建应用程序时,您要做的第一件事就是
var app = angular.module("myApp");
and then 然后
app.directive(...);
app.controller(...);
app.service(...);
If you have a look at the angular-seed which is neat starter app for angular, they have separated out the directives, services, controllers etc into different modules and then loaded those modules as dependancies on your main app. 如果您看一下angular-seed这是一个整洁的角度启动应用程序,他们已将指令,服务,控制器等分离到不同的模块中,然后将这些模块作为依赖项加载到您的主应用程序上。
Something like : 就像是 :
var app = angular.module("myApp",["Directives","Controllers","Services"];
Angular also lazy loads these modules ( into memory) not their script files. Angular也懒得加载这些模块(进入内存)而不是他们的脚本文件。
In terms of lazy loading script files, to be frank unless you are writing something extremely large it would be an overkill because angular by its very nature reduces the amount of code you write. 就延迟加载脚本文件而言,坦率地说,除非你写的东西非常大,否则它将是一种矫枉过正,因为角度本质上减少了你编写的代码量。 A typical app written in most other frameworks could expect a reduction in around 30-50% in LOC if written in angular. 如果以角度编写,在大多数其他框架中编写的典型应用程序可以预期在LOC中减少约30-50%。
As @ganaraj mentioned AngularJS has dependency injection at its core. 正如@ganaraj所提到的,AngularJS的核心是依赖注入。 When building toy seed applications with and without RequireJS, I personally found RequireJS was probably overkill for most use cases. 在使用和不使用RequireJS构建玩具种子应用程序时,我个人发现在大多数用例中,RequireJS可能有点过分。
That doesn't mean RequireJS is not useful for it's script loading capabilities and keeping your codebase clean during development. 这并不意味着RequireJS对于它的脚本加载功能没有用,并且在开发过程中保持代码库的清洁。 Combining the r.js optimizer ( https://github.com/jrburke/r.js ) with almond ( https://github.com/jrburke/almond ) can create a very slim script loading story. 将r.js优化器( https://github.com/jrburke/r.js )与杏仁( https://github.com/jrburke/almond )结合起来可以创建一个非常纤薄的脚本加载故事。 However since its dependency management features are not as important with angular at the core of your application, you can also evaluate other client side (HeadJS, LABjs, ...) or even server side (MVC4 Bundler, ...) script loading solutions for your particular application. 但是,由于它的依赖关系管理功能对于应用程序核心的angular不是那么重要,您还可以评估其他客户端(HeadJS,LABjs,...)甚至服务器端(MVC4 Bundler,...)脚本加载解决方案为您的特定应用。
Yes, it makes sense. 是的,这是有道理的。
Angular modules don't try to solve the problem of script load ordering or lazy script fetching. Angular模块不会尝试解决脚本加载排序或延迟脚本提取的问题。 These goals are orthogonal and both module systems can live side by side and fulfil their goals. 这些目标是正交的,两个模块系统可以并存并实现其目标。
Source: Angular JS official website 资料来源: Angular JS官方网站
Yes it makes sense to use requireJS with Angular, I spent several days to test several technical solutions. 是的,将angularJS与Angular一起使用是有意义的,我花了几天时间来测试几种技术解决方案。
I made an Angular Seed with RequireJS on Server Side. 我在服务器端使用RequireJS制作了一个Angular Seed。 Very simple one. 非常简单。 I use SHIM notation for no AMD module and not AMD because I think it's very difficult to deal with two different Dependency injection system. 我使用SHIM表示法没有AMD模块而不是AMD,因为我认为处理两个不同的依赖注入系统非常困难。
I use grunt and r.js to concatenate js files on server depends on the SHIM configuration (dependency) file. 我使用grunt和r.js来连接服务器上的js文件取决于SHIM配置(依赖)文件。 So I refer only one js file in my app. 所以我在我的应用程序中只引用了一个js文件。
For more information go on my github Angular Seed : https://github.com/matohawk/angular-seed-requirejs 有关更多信息,请访问我的github Angular Seed: https : //github.com/matohawk/angular-seed-requirejs