【问题标题】:Unknown provider: $routeParamsProvider <- $routeParams未知提供者:$routeParamsProvider <- $routeParams
【发布时间】:2016-06-10 07:28:54
【问题描述】:

我目前正在通过创建一个简单的播客应用程序来学习 AngularJS 和 Ionic。 我正在尝试使用 routeParams 获取“itemId”,但出现以下错误:

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

现在这就是我传递“itemId”的方式

  .state('ted', {
    url: '/ted/:itemId',
    templateUrl: 'templates/ted-talks.html',
    controller: 'DetailsController'
  })

这是我的控制器:

starter.controller("DetailsController", ["$scope", "$routeParams", "$http", function ($scope, $routeParams, $http) {
  $http.get('http://api.npr.org/query?id=57&apiKey={I've taken the ID off})
  .success(function(data, status, headers, config){
    var x2js = new X2JS();
    var jsonOutput = x2js.xml_str2json(data);
    console.log(jsonOutput);

     $scope.stories = jsonOutput.nprml.list.story;
     
     if($routeParams.itemId) {
      console.log('Single page id' + $routeParams.itemId);
     }


  })
  .error(function(data, status, headers, config){
    alert('There is a problem');
  })
}]);

任何想法是什么导致了这个错误?我相信 routeParams 已经包含在 ionic 框架中,因为它们提供的演示似乎可以工作,但我不知道如何。

非常感谢任何帮助:)

【问题讨论】:

    标签: javascript angularjs routing angular-ui-router angularjs-routing


    【解决方案1】:

    当你使用 Angular-ui-router 时,你应该使用 $stateParams 依赖而不是 $routeParams 用于 ui-router(Angular ui-router $stateProvider)

    if($stateParams.itemId) {
        console.log('Single page id' + $stateParams.itemId);
    }
    

    $routeParams 可用于 ngRoute 模块(AngularJS 路由$routerProvider)

    【讨论】:

    • 你不知道我为此纠结了多久。非常感谢!试图将参数传递给我的控制器,但没有任何工作哈哈....
    • @dannio 很高兴知道我提供了帮助。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 2016-11-29
    • 2018-09-28
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多