user3412128
user3412128

Reputation: 11

Angular unknown provider when trying to get a constant from another module

Just started on AngularJS, and it has been a challenging ordeal so far.

My problem today is I'm trying to configure a controller through a variable on the URL. I don't want the "real" controller to have to know where a given parameter came from, so long as it's there. The main app controller is therefore responsible of getting the parameter from the URL, and setting a constant that the "real" controller will use.

For the life of me, I cannot see what I am doing wrong in the initialization. Any help would be greatly appreciated (including what are standard practices to troubleshoot these kind of issues :))

Here is the html:

<!DOCTYPE html>
<html ng-app="myApp">
    <head>
    <!-- the base tag is required for th Angular.js $location.search() function to work correctly -->
    <base href='/' />
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>
    <script>
        angular.module("myController.constants", []);
        angular.module("myApp", ["myController", "myController.constants"], function($locationProvider) {
            $locationProvider.html5Mode(true);
        })
        .controller("myAppCtrl", ['$scope', '$location', function ($scope, $location) {
            var searchObject = $location.search();
            angular.module("myController.constants").constant('myConstant', searchObject['theConstant']);
        }]);
     </script>
    <script src="js/controllerExample.js"></script>
</head>
<body ng-controller="myAppCtrl">
    <div ng-controller="myControllerCtrl">
        <p>The constant is {{theConstant}}</p>
    </div>
</body>
</html>

Here is the js for the controller:

angular.module("myController", ["myController.constants"])
.controller("myControllerCtrl", ['$scope', 'myConstant', function ($scope, myConstant) {
    $scope.theConstant = myConstant;
}]);

With the code above, I keep getting this error message

Error: [$injector:unpr] Unknown provider: myConstantProvider <- myConstant <- myControllerCtrl

Thanks!

Upvotes: 1

Views: 2804

Answers (1)

Komo
Komo

Reputation: 2138

I could be mistaken but I don't think you can declare a module inside a controller declaration. Try putting

angular.module("myController.constants").constant('myConstant', searchObject['theConstant']); 

outside "myAppCtrl" declaration.

Upvotes: 1

Related Questions