Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: refactoring controllers turn $routeParams to undefined

Tags:

angularjs

I'm splitting my angular code in different modules and I'm facing an issue. I suppose I don't understand the internals of services injection.

going from controllers defined as global functions

angular.module('foo', []);
function fooCtrl($scope, $routeParams) {

to a declarative form in order to have them as part of a module

angular.module('foo',[]).
controller(['fooCtrl', function($scope, $routeParams) {

in the end I lose the $routeParams service (undefined). Do I need to inject explicitly my $routeParams to the module? How do I do it?

like image 495
plus- Avatar asked Dec 05 '25 17:12

plus-


1 Answers

It appears like you are not calling your controller() correctly.

angular.module('foo',[]).
controller("fooCtrl", <-- controller name
    ['$scope', '$routeParams', <-- list of dependencies
    function($scope, $routeParams) { <--actual controller function
       alert($routeParams);
}]);

Example on jsfiddle

If you don't plan on doing minification you can also do the following:

angular.module('foo',[]).controller("fooCtrl", function($scope, $routeParams) {   
});
like image 178
Mark Coleman Avatar answered Dec 08 '25 09:12

Mark Coleman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!