Search code examples
javascriptangularjsurl-routing

can't access $routeParams in my angularjs controller


            var myApp = angular.module('myApp', ['ngGrid', 'ngRoute']);

            myApp.config(['$routeProvider', function ($routeProvider)
            {
                $routeProvider.
                when('/:energyId/:energyChain/:resourceId/:facilityId',
                {
                    templateUrl: '/Content/resourceTemplate.html',
                    controller: 'detailsController'

                }).
                otherwise({
                    redirectTo: '/param1/param1/param1/param1'
                });
            }]);

            myApp.controller('detailsController', ['$scope', function ($scope,$routeParams) {

                //here the error when i add the following single statement 
                $scope.status = $routeParams.energyID;//This cuases angular to fail but only this controller 

                $scope.models = [
                          { Parameter: "Bob", Period: 25, Values: "NewYork", Scale: "NewYork", Unit: "NewYork", Per: "NewYork" },
                         { Parameter: "Bob", Period: 25, Values: "NewYork", Scale: "NewYork", Unit: "NewYork", Per: "NewYork" },
                         { Parameter: "Bob", Period: 25, Values: "NewYork", Scale: "NewYork", Unit: "NewYork", Per: "NewYork" },
                         { Parameter: "Bob", Period: 25, Values: "NewYork", Scale: "NewYork", Unit: "NewYork", Per: "NewYork" },
                         { Parameter: "Bob", Period: 25, Values: "NewYork", Scale: "NewYork", Unit: "NewYork", Per: "NewYork" }

                                ];


            }]);

I have tested my code and it worked until I added the status variable in the controller which access the $routeParams. the error shows cant access energyID


Solution

  • You are using Angular's 'minifiyable' syntax but not supplying $routeParams as one of the arguments. This line:

    myApp.controller('detailsController', ['$scope', function ($scope,$routeParams) {
    

    Should be:

    myApp.controller('detailsController', ['$scope', '$routeParams', function ($scope,$routeParams) {