controller无法识别从routeProvider传递的routeParams变量

时间:2014-08-05 16:48:29

标签: javascript angularjs angularjs-routing

  <script>
    var app= angular.module('myApp', ['ngRoute', 'ngResource']);

    app.factory('Greeter', ['$resource',function($resource){
      return $resource(
        'http://123.com/processor-url.php',{
        },{
          query: {
            method:'GET',
            /*params: { myvar: myvarActual },*/
            isArray:true

          }
        }
      );
    }]);

    app
    .controller('appointmentController', ['$scope', 'Greeter',function($scope,$routeParams,Greeter){
      alert($routeParams.myvar);
      Greeter.query(function(data) {
        $scope.output = data.myvar;
      });

    }]);

    app.controller('homeController', ['$scope', function($scope){
    }])


    /*Final Config After Loading Everything*/
    app.config(['$routeProvider', function($routeProvider) {
      $routeProvider.when('/appointments/:myvar', {templateUrl: 'appointments.html', controller: 'appointmentController'});
      $routeProvider.when('/home', {templateUrl: 'home.html', controller: 'homeController'});
      $routeProvider.otherwise({redirectTo: '/home'});
    }]);
  </script>

在上面的代码中,似乎控制器甚至无法识别myvar中包含的routeProvider's myvar。由于提醒会显示undefined,而不是myvar的实际值(即... /appointments/1066 ...如果是,我希望$routeParams.myvar = 1066和{{1}应该是1066

1 个答案:

答案 0 :(得分:1)

依赖项声明中缺少

$routeParams,所以目前它正在寻找myvar上不存在的Greeter ......

app
.controller('appointmentController', ['$scope', 'Greeter',function($scope,$routeParams,Greeter){

应该......

app
.controller('appointmentController', ['$scope', '$routeParams', 'Greeter', function($scope, $routeParams, Greeter){