Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerModule with dependencies

I'm building an app with MEAN.JS and I'm trying to use a controller from another module. I've found that I can do this with dependency injection:

angular.module(‘MyModule’, [‘Dependency’]);

But the way modules are created in MEAN.JS is:

ApplicationConfiguration.registerModule('MyModule');

And I can't just pass a second parameter to registerModule. So, how should I do this? Do I have to use both methods? Am I doing it wrong?


Example

I want to add a new model: Campaign. Campaigns are created by admins only, but can be seen by the "campaign owner" (another User). The create campaign form should list available Users, so the admin can select the one that's going to be the "owner" of that Campaign.
The problem is, the create campaign form is controlled by CampaignsController, how can I list Users? I've used another controller (UsersController) and thats the problem, it is undefined because we are in the Campaign module.


EDIT:

The problem was grunt autorestarting the app incorrectly:
I moved the controller from one module (folder) to another, but grunt was still trying to load it from the old path, and thus failing: Controller not found. I thought the problem was dependency injection, but you only have to close grunt (Ctrl+C) and run it again. Problem solved.
Anyways, thanks for the answer @gilBirman cause it is correct: there is no need to inject anything with MEANJS.

like image 373
Salvatorelab Avatar asked Jul 07 '14 08:07

Salvatorelab


2 Answers

MEAN.js makes all the modules registered via registerModule available to all other modules in your app by adding it as a dependency to the main app called mean. Here's the part of the MEAN.js source code that does this:

var applicationModuleName = 'mean';
....
// Add a new vertical module
var registerModule = function(moduleName) {
    // Create angular module
    angular.module(moduleName, []);

    // Add the module to the AngularJS configuration file
    angular.module(applicationModuleName).requires.push(moduleName);
};

So you're on the right track, however it sounds like you are trying to inject a controller. However, in angular, controllers are not injectable. You can inject services, factories, values, and constants.

like image 162
Gil Birman Avatar answered Sep 19 '22 13:09

Gil Birman


First create your own module for example:

angular.module('app.controllers', []) - angular module with controllers.

then add controller to that module:

angular.module('app.controllers', [])
       .controller('dashboard.admin.account.controller', ['$scope', ($scope) { .... }]);

then create global module which will bind to your markup:

angular.module('app', [
  'app.controllers'
  'ui.router',
  'ngAnimate'
]);

then bootstrap your global module to markup:

domReady(function () {
  angular.bootstrap(document, ['app']);
});

Now you can use your controller.

like image 38
Vitaliy Khudonogov Avatar answered Sep 20 '22 13:09

Vitaliy Khudonogov