0

So I am having troubles with managing dependencies for my node app. The structure is the following

app.js

var app = angular.module('myApp', ['myController', 'myFactory', 'rzModule', 'chart.js', 'myService', 'selector']);

myController.js

var app = angular.module('myApp', ['chart.js', 'rzModule', 'selector']);
app.controller('myController', function($scope, $http, $rootScope, myFactory, mySecondService){ ... }]);

myFactory.js

angular.module('myApp').factory('myFactory', function($http, $rootScope){ ... });

myService.js

angular.module('myApp').service('myService', function($http){...});

Above structure works, the app runs as expected. However I don't think what I am doing in myController.js is correct, because I now want to add another dependency and I can't figure out how to do so without the app crashing.

Question 1: In above myController.js, I think I am creating a new module instead of reusing the one from app.js, is that correct? If so, moving the 3 dependencies inline like the following results in "myController is not available":

var app = angular.module('myApp');
app.controller('myController',['chart.js', 'rzModule', 'selector', function(...)

Why does this not work? According to the documentation it should.

Question 2: I want to add angular-moment to the myController. The instructions say I should add 'angularMoment' as dependency to the app.js, and then 'moment' to the controller dependencies. If I do so and add 'moment' inline like in Question 1, I am getting again the "myController is not available" error.

6
  • app.controller('myController',['chart.js', 'rzMo.... is the way to proceed, What is the sequence of the files? Commented Jun 15, 2017 at 10:01
  • @Satpal that was my guess, but as mentioned, it results in the controller not being available. Commented Jun 15, 2017 at 10:02
  • why do you use myController inside your angular.module('myApp' decleration? Commented Jun 15, 2017 at 10:02
  • 1
    @ffritz that's because you need to remove 'myController' from here var app = angular.module('myApp', ['myController' Commented Jun 15, 2017 at 10:03
  • So services/factories should not be included in the app dependencies? Commented Jun 15, 2017 at 10:07

3 Answers 3

2

So before answering your questions quick thing as you proceed with angularjs, modules need to be injected into the app not into the controller, so any module to be included need to be injected in the app.

var app = angular.module('myApp', [<all modules comma separated>]);

Similarly controller can have all those dependencies which are either linked to the myApp(like services, factories and others) and those services which are defined in modules which are injected into myApp

you cannot inject controllers, services in myApp only modules can be injected there

Question1

var app = angular.module('myApp', ['chart.js', 'rzModule', 'selector']);
app.controller('myController', function(...)

this shold work and using this it not meant you are creating any new app

Question2

var app = angular.module('myApp', ['angularMoment']);
app.controller('myController', ['moment', function(...)

like this you can use moment object.

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

2 Comments

Thanks a lot. So regarding angularMoment, it needs to be injected both in app.js and in the respective controller using it? Same goes for any other module used by a controller/service? I adapted my code according to this, and now have the issue that myService is undefined in myController, and injecting it right next to moment doesn't fix it.
--Update: Seems like when inline injecting a service, you also have to inject all others like $scope and $http. Works now!
1

As Satpal has said

app.controller('myController',['chart.js', 'rzMo

Is the way to go, or a cleaner way to write this would be to do something like this.

angular.module('myApp').controller('myController');

myController.$inject = ['$scope', '$http', '$rootScope', 'myFactory', 'mySecondService']

function myController($scope, $http, $rootScope, myFactory, mySecondService){}

The reason myController is not available with the fixes is that it's now you're not overwriting the main application module and it's looking for a module called myController which can't be found (as it's a controller and not a module). So remove 'myController' from var app = angular.module('myApp', ['myController'

I suggest for you to take a look at John Papa's Style Guide for AngularJS it can be helpful for writing clean code.

2 Comments

Thanks, however first I think the injection should look like this: myController.$inject = ['$scope' ...]. Second this leads to The controller with the name 'myController' is not registered. for me.
@ffritz You're right, forgot to change it when I was copying it from your code. I think Lalit Sachdeva's answer is a better explanation tbh.
0

Ans 1: Yes in the second case you are trying to define it again. To define an app use:

angular.module('myApp', [..list of dependancies...]);

Now if you want to define controllers on the previous module use

angular.module('myApp').controller();

Now comes injecting your 3 dependancies, move them to the top where you have defined your app.

Ans 2: You will have to inject angularMoment to you app and moment to your controller.

The best way to handle dependancy injection in angular is to use $inject. for example you controller will look something like

angular.module('myApp').controller('myController', myController);

myController.$inject = ['moment','$scope', '$http', '$rootScope', 'myFactory', 'mySecondService']
function myController(moment, $scope, $http, $rootScope, myFactory, mySecondService){
    //your controller code.
}

or else you can try the following as well

angular.module('myApp').controller('myController',['moment','$scope', '$http', '$rootScope', 'myFactory', 'mySecondService', function myController(moment, $scope, $http, $rootScope, myFactory, mySecondService){
    //your controller code.
}]);

The following will also work but not recommended;

angular.module('myApp').controller('myController',function myController(moment, $scope, $http, $rootScope, myFactory, mySecondService){
        //your controller code.
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.