0

Let's say we have a few dozen "general purpose" utilities in the form of angular directives, like this one for ignoring typed characters other than 0-9.

How do we set up a file containing those directives so that it would be easy to include them in any angular module, and how do we include them?

Do we just create a module Foo to hold these directives

     var FOO = angular.module('Foo', []);
     FOO.directive('stripExcessWhiteSpace', function() {
        return {
           <snip>
         }
     });

and then include that module in our current app? Does the name of that file have to be Foo.js and does the file have to be in the same folder the file for our current app is found in?

     var app = angular.Module('Customers', ['Foo']);

or is there more to it?

Community
  • 1
  • 1
Tim
  • 8,669
  • 31
  • 105
  • 183

1 Answers1

0

Yes, you create a module for it. And you place the code of the directives in a JS file that will have to be included in the HTML file of the application like any other JS file. The JS file name doesn't have any importance. What matters is the name of the module.

Since this module is supposed to be reusable, you should choose a name that has a very low chance of conflicting with a module that an app might have already chosen (like 'controllers' or 'services'). Prefixing the module name with a string that identifies yourself or your company is a good idea:

angular.module('myCompany.utils')
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255