Brk Brk - 2 years ago 86
AngularJS Question

angular module namespace usage

I would to understand the use of angular module more clearly and deeply.
I have a folder of directives where each directive is on separate file.

My first file(first directive) has a declaration of


My second file has a declaration of


My third file has a declaration of


and so on.

There for my references of files is like this.

<script src="FirstFile"></script>
<script src="SecondFile"></script>
<script src="ThirdFile"></script>

I have a couple of questions:

1.How can I get rid of angular.module("Name") declaration in every file?

  1. why my first declaration have to be angular.module("Name",["Dep1","Dep2"]) with list of dependencies, can I call this file second in row?

Answer Source

Your first file is declaring the angular module. When you pass a second parameter to the angular.module function, you are declaring it. Without the parameter, you are referencing it.

Declare module:

angular.module('moduleName', ['dep1', 'dep2'])

Reference module:


A module can only been declared once.

Once it is declared, you can "attach" controllers and services etc. to it.

As for you second question, it is generally okay and considered good practice to reference the module in the way you do (angular.module("name")).

However, if you really don't want to, you can save it to a variable.

var myModule = angular.module('name');

module.directive('directiveName', ...);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download