无法在我的angularjs控制器中访问$ routeParams

时间:2015-07-12 12:46:56

标签: javascript angularjs url-routing

            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" }

                                ];


            }]);

我已经测试了我的代码,直到我在访问status的控制器中添加了$routeParams变量。错误显示无法访问energyID

1 个答案:

答案 0 :(得分:1)

您正在使用Angular的'minifiyable'语法,但不提供$routeParams作为参数之一。这一行:

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

应该是:

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