Fred J.
Fred J.

Reputation: 6039

angular defining/loading/configuring the modules

What are the differences between the 2 ways to write the code below in terms of functionality?

I am adhering to this code writing style.

I tried in my code but the second form broke the code, I refrained from posting the whole code to focus on the main part. Thanks

var myApp = angular.module('MainMenuCtrl', ['ngAnimate']);
myApp.controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);

 angular
    .module('MainMenuCtrl', ['ngAnimate'])
    .controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);

Upvotes: 0

Views: 27

Answers (1)

charlietfl
charlietfl

Reputation: 171690

The second approach is more modular since you can grab a part of it and immediately put it into another project without having to see if the app variable, which happens to be a global, matches the project you insert it into.

Also you can wrap all components in an IIFE and include "use strict" without forcing it on any other scripts in page.

Also build and scaffolding tools don't need to set any variables

// in one file
;(function(){

    "use strict";
     // var app wouldn't be available in the next file if it was used here

     angular
        .module('MainMenuCtrl', ['ngAnimate'])
        .controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);
)}();

// in another file
;(function(){    
    "use strict";

     angular
        .module('MainMenuCtrl')
        .controller('AnotherCtrl', ['$scope', '$http',AnotherCtrl]);

)}();

Upvotes: 1

Related Questions