0

New to Angular, trying to figure out how I should modularize my app.

app
    toolbar
        toolbar.module.js
        menu.html
        index.html
        search.html
    sub-system-1
        subSystem1.module.js
        directive-template.html
    sub-system-2
        subSystem2.module.js
        directive-template.html

The toolbar.module.js for example has 3 directives (menu, index, search) and each directive has its own controller, service. All the controllers, directive definitions and services are defined in the toolbar.module.js...kind of confusing when it comes to maintaining the code.

Is it possible to come up with a finer grained setup such that each directive is enclosed in its own file along side its controller & service?

I figure I could add:

        menu.directive.js

And inside there have something like

var module = angular('toolbar.menu.directive',[]);
module.directives(...);//add menu directive to the module

Then inside toolbar.modules have something like:

var module = angular('toolbar',[toolbar.menu.directive]);

Is this the only way I can achieve this by defining lots of fine-grained modules?

1 Answer 1

1

No, you don't have to define tons of tiny modules. In Angular, you can reference an existing module by omitting the second argument:

angular.module('moduleName');

By doing this, you can split your various toolbar directives, controllers, and services into separate files and in each file simply reference the toolbar module like so:

angular.module('toolbar')
  .controller('MenuCtrl', function() {
    // ...
  });
Sign up to request clarification or add additional context in comments.

1 Comment

You've just got to make sure that they get run in the right order. If angular.module('moduleName'); gets run before angular.module('moduleName', []); then you'll get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.