1

I have each of my angular controllers/services in separate folders (taken from angular seed). I need to call different controllers on different pages.

I am getting errors that ***Ctrl is not loading. How do I 'inherit' controllers specific to each page?

Here is my current structure.

// Index.html
<script>app.js</script>
<div ng-app='myApp'>
    <div ng-controller='headerCtrl'> Load user profile on every page</div>
</div>


// TaskList.html
<script>app.js</script>
<script>task.js</script>
<div ng-app='myApp'>
    <div ng-controller='headerCtrl'>Load user profile on every page</div>
    <div ng-controller='taskCtrl'>  Load task list </div>
</div>


// ProjectList.html
<script>app.js</script>
<script>project.js</script>
<div ng-app='myApp'>
    <div ng-controller='headerCtrl'>Load user profile on every page</div>
    <div ng-controller='projectCtrl'>Load task list</div>
</div>


// app.js
var myApp = angular.module('myApp', []);
myApp.controller('headerCtrl', function ($scope, $window) {
    // do stuff
});


// task.js
var taskCtrl = angular.module('taskCtrl', []);
taskCtrl.controller('taskCtrl', function ($scope, $window) {
    // do stuff
});

// project.js
var projectCtrl = angular.module('projectCtrl', []);
projectCtrl.controller('projectCtrl', function ($scope, $window) {
    // do stuff
});

2 Answers 2

1

Unless you have a good reason to do, I would recommend just putting your controllers in the same module. You can reference your taskApp module in other files (task.js and project.js) by doing the following:

var taskApp = angular.module('taskApp');
taskApp.controller('taskCtrl, ...);

Note that the call to angular.module has only one parameter - that's telling Angular that you want to get a reference to an existing module, rather than create a new module. On the other hand, doing this:

var taskApp = angular.module('taskApp', []);

Is telling Angular that you want to create a new module (with no dependencies). Subtle but very important difference.

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

2 Comments

I would like to keep 'domain logic' separated. So I have create separate folders/js files to each area of the site ( Tasks, Projects, Members, etc ... )
@RaviR I agree with you. That's the right way to build large applications. Have you found out the right way to do this? If yes, how did you proceed? Please share.
1

That's because you're putting each controller in a separate module, but only referencing myApp , you need to inject the other app dependencies to your main app. Also, why are you storing your app and controllers in the same object? No wonder you're getting errors. Do this instead:

var taskApp = angular.module('taskApp', []);
taskApp.controller('taskCtrl', function ($scope, $window) {
    // do stuff
});

var projectApp = angular.module('projectApp', []);
projectApp.controller('projectCtrl', function ($scope, $window) {
    // do stuff
});

var myApp = angular.module('myApp', ['taskApp','projectApp']);

4 Comments

When I load this var myApp = angular.module('myApp', ['taskApp','projectApp']); within the Index.html, I get an error since each controller is in a separate js file. taskApp = task.js projectApp = project.js
You need to reference each file in your index.html then, after you reference angular of course, either that or just put them in the same file. As Sergey said, unless you're building huge apps, then i would just put the controllers in the same module.
I am building a 'large' app .. about 12-15 domain objects. So I would like to separate folders/js files.
Also, if I added all the dependencies I would need to load about 15 js files, most of which code will not be used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.