1

If I declared module inside an IIFE in Angular JS

(function(){
  var app=angular.module("myapp",[]); 

  var mapController = function () {
    var mapScope = this;

  };
  app.controller("mapController", [mapController]);
})()

How is it going to be accessible to another controller, which is defined inside another IIFE?

Suppose I have another controller in a different JS file table controller. How can I access app module there?

app.controller("tableController", ["$http", tableController,commonService]);

In other words, can we create angular JS app without a single global variable where control are separated in multiple JS files?

3
  • What needs to be accessible in other controller? this controller (mapController)? Commented Mar 6, 2017 at 18:38
  • var app=angular.module("myapp",[]); , this need to be accessible in another controller. Simplest way is I will declare it a global variable and it's running fine. But how can I access it , when it's declared inside an IFFY. Commented Mar 6, 2017 at 18:41
  • Just because you are in an IIFE doesn't mean angular doesn't have access to the window. See the code github.com/angular/angular.js/blob/master/src/module.suffix Commented Mar 6, 2017 at 18:45

1 Answer 1

3

In the case where you want to access the module in the other controller, dont use the variable but instead use the module definition like this:

angular.module("myapp").controller("tableController", ["$http", tableController,commonService]);

This is because your var app is inside an IIFE which wont be accessible in the other controller file. But the module definition will work subject to the condition that it is already defined somwhere.

Sign up to request clarification or add additional context in comments.

1 Comment

Working fine , Thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.