Drifter Drifter - 4 months ago 16x
AngularJS Question

AngularJS and IIFE

I have started learning AngularJS and while going through some code samples I came across the following:

(function(app) {
'use strict';
app.directive('sideBar', sideBar);
function sideBar() {
return {
restrict: 'E',
replace: true,
templateUrl: '/scripts/spa/layout/mypage.html'

The above code creates a custom directive using IIFE. I am very confused about the last line of the code. It is passing a module named common.ui. Can someone explain me how this way of passing a parameter works and how this can be rewritten in a different way?


angular.module('common.ui') becomes referenced by app. See What is the (function() { } )() construct in JavaScript?.

You can declare it normally

(function() {
    var app = angular.module('common.ui');