2015-07-12 63 views
0
  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" } 

           ]; 


      }]); 

我已經測試我的代碼和它的工作,直到我在訪問$routeParams控制器增加了status變量$ routeParams。錯誤顯示無法訪問energyID不能訪問我的angularjs控制器

回答

1

您正在使用Angular的'minifiyable'語法,但不提供$routeParams作爲參數之一。這條線:

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

應該是:

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

效果很好,感謝男人@sdgluck –