0

I'm looking at managing my AngularJS code using RequireJS and have come up with the following pattern:

"use strict";

(function () {

    var moduleName = "module.name";

    define(
        [
            'angular'
        ],
        function (
            ng
        ) {

            ng.module(moduleName, []);

            return moduleName;

        }
    );

})();

I guess the general idea is that whenever my module/controller/etc... is included, rather than returning the object itself, it binds it's name into Angular and then returns that handle to the caller.

One question I have - Would it be a better idea to return the array parameter I'm passing to ng.module and then eliminate the call, allowing the "includer" of this module to decide what name to bind to?

Are there any other pitfalls or recommendations for designing nicely encapsulated AngularJS+RequireJS modules that could be applied to this pattern? Anything I should watch out for to avoid hardship or confusion in the future?

Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112
  • Have tried your code to see if it is working after angular.bootstrap? I am pretty sure that modules created using your pattern after bootstrap cannot be injected into your app. Take a look at my question here http://stackoverflow.com/questions/19134023/lazy-loading-angularjs-modules-with-requirejs – marcoseu Apr 02 '14 at 06:53
  • 1
    Actually that same pattern is the one I use to create AngularJS apps. Checkout the example code at my Github: http://github.com/leog/epsilon – leog Sep 01 '14 at 01:00

0 Answers0