$ routeParam不通过url使用$ resource factory传递变量

时间:2014-08-05 14:13:26

标签: javascript angularjs angularjs-resource angularjs-factory angularjs-ng-route

代码

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

app.factory('Greeter', ['$resource',function($resource){
  return $resource(
    'http://123.com/processor-url.php',{
      /*inputName:inputName,*/
      callback: 'JSON_CALLBACK'
    },{
      query: {method:'GET',isArray:true}
    });
}]);

app
.controller('appointmentController', ['$scope','routeParams', 'Greeter',
  function($scope,$routeParams,Greeter){
    /*alert("yes");*/
    alert($routeParams);
    var x = Greeter.query({
        myvar: $routeParams
    });
    $scope.output = x;
}]);

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'});
}]);

数据(来自http://123.com/processor-url.php,当myvar = 1066时)

[
    {
        "myCodeId": "1066",
        "myCodeName": "session test",
        "myCodeOwner": "dvg",
        "myCodeBody": "",
        "myCodeTimesUsedCount": "0",
        "myCodeValueAsMins": "0",
        "myCodeCreateDate": "2014-07-30 11:04:58",
        "myCodeLastEditDate": "2014-07-30 11:05:43",
        "isSnippet": "0",
        "isDeleted": "0",
        "isMyFavorite": "0"
    }
]

我使用此代码运行+假设为myvar = 1066的结果的数据。

会有错误
TypeError: Cannot read property 'query' of undefined
    at new <anonymous> (.../urlTest/:29:24)

在哪一行

var x = Greeter.query({

我想知道错误在哪里?正如我在控制器本身内注入Greeter一样。

==更新1] ===

我通过将所有routeParam修复为routeParams来更新代码。

以前的错误已解决。但是,现在错误变为

Error: [$injector:unpr] Unknown provider: routeParamsProvider <- routeParams
http://errors.angularjs.org/1.2.0-rc.3/$injector/unpr?p0=routeParamsProvider%20%3C-%20routeParams

我可能以错误的方式使用routeParams?因为我想要做的是我假设routeParams将是1066的值,因为这是&#39; /&#39;之后的内容。符号。我理解其功能是错误的吗?

由于

1 个答案:

答案 0 :(得分:1)

我清楚看到的一个问题是DI的参数不匹配。这个

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

应该是

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

服务名称也是$routeParams