Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Angular Guidelines when using WebPack

No more IIFE

  • WebPack wraps each requireed code with a function, so there's no need to wrap our code with an IIFE
/**
 * Good
 */

'use strict';

function TestController($scope) {

}

module.exports = TestController;

/**
 * Bad
 */
 
(function () {
	'use strict';

  function TestController($scope) {
  
  }
  
  module.exports = TestController;
}());

Dependency Injection

/**
 * Good
 */

'use strict';

function TestController($scope) {

}

module.exports = TestController;

/**
 * Bad
 */

'use strict';

function TestController($scope) {

}

module.exports = ['$scope', TestController];

/**
 * Bad
 */

'use strict';

function TestController($scope) {

}

TestController.$inject = ['$scope'];

module.exports = TestController;
  • In special cases where creating things dynamically, add a /* @ngInject */ comment right before the function that needs to be annotated (see Explicit annotations with ngInject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment