0

I've seen this done two ways:

  1. The module is assigned a global variable which is then appended to directives, controllers, and services:

    var app = angular.module('myApp', [ ]);
    
    app.directive('myDirective' function() {
       // directive stuff here
    });
    
  2. Or the the module is used as-is like:

    angular.module('myApp', [ ])
    
    .directive('myDirective' function() {
       // directive stuff here
    });
    

So why would you do it one way or the other?

Philippos Slicher
  • 395
  • 1
  • 5
  • 19
jami0821
  • 301
  • 2
  • 16
  • I'm really surprised the duplicate didn't show up when you typed in your title. – George Stocker Nov 10 '15 at 17:38
  • hmmm.. me as well. Thanks for that! – jami0821 Nov 10 '15 at 17:39
  • Readability-wise, #1 looks better to me. Chaining periods together (`angular.module().directive`) can get hard to read and violates the "law of demeter" that Misko Hevery talks about (Misko is the original angularjs author) – Zack Macomber Nov 10 '15 at 17:40
  • @ZackMacomber popular style guides recommend the second approach using `module()` as getter. Components don't have to be chained, you can use a getter anywhere – charlietfl Nov 10 '15 at 18:07

0 Answers0