Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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. Does it make sense to do the same with Angular.js?

share|improve this question
15  
I highly recommend watching this official AngularJS video. It is bookmarked at the part where they talk about RequireJS. – mattblang Dec 13 '12 at 21:30
Just my two cents : basarat.com/2013/05/angularjs-requirejs.html – BASarat May 19 at 9:03

7 Answers

Yes it makes sense to use angular.js along with require.js wherein you can use require.js for modularizing components.

I can point you to a seed project which uses both angular.js and require.js. Hope that helps!

share|improve this answer
26  
The seed project mentioned above has not been touched for a year so I've created a new one using latest AngularJS and RequireJS with full support for testacular-driven testing. – tnajdek Feb 25 at 19:44
1  
Your comment in main.js saved my day... Thanks a lot! – Ievgen Martynov May 6 at 16:30
@tnajdek, I updated the link in Anshu's answer to point to the one you suggest. – David Rivers May 24 at 14:24

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

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.

Something like :

var app = angular.module("myApp",["Directives","Controllers","Services"];

Angular also lazy loads these modules ( into memory) not their script files.

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.

share|improve this answer
Indeed, it's better configure services in Angular.js than load modules with Require.js. This makes it easier to play with the $scope and services, as I played with Socket.io – markotom Dec 29 '12 at 6:13

Yes, it makes sense.

"Angular modules don't try to solve the problem of script load ordering or lazy script fetching. These goals are orthogonal and both module systems can live side by side and fulfil their goals". source: Angular JS official website.

share|improve this answer

As @ganaraj mentioned AngularJS has dependency injection at its core. When building toy seed applications with and without RequireJS, I personally found RequireJS was probably overkill for most use cases.

That doesn't mean RequireJS is not useful for it's script loading capabilities and keeping your codebase clean during development. 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. 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.

share|improve this answer

It makes sense to use requirejs with angularjs if you plan on lazy loading controllers and directives etc, while also combining multiple lazy dependencies into single script files for much faster lazy loading. RequireJS has an optimisation tool that makes the combining easy. See http://ify.io/using-requirejs-with-optimisation-for-lazy-loading-angularjs-artefacts/

share|improve this answer

Yes it makes sense to use requireJS with Angular, I spent several days to test several technical solutions.

I made an Angular Seed with RequireJS on Server Side. 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.

I use grunt and r.js to concatenate js files on server depends on the SHIM configuration (dependency) file. So I refer only one js file in my app.

For more information go on my github Angular Seed : https://github.com/matohawk/angular-seed-requirejs

share|improve this answer

No it does not make any sense. It just lets you do things twice. It is possible, yes, but it does not make any sense...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.