ngRoute $ injector:unpr未知提供者

时间:2015-07-31 03:23:54

标签: angularjs angular-routing

这是我的代码:

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

myApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  when('/list', {
    templateUrl: 'includes/list.html',
    controller: 'artistListController',
    controllerAs: 'alc',
  }).
  when('/detail/:id', {
    templateUrl: 'includes/detail.html',
    controller: 'artistDetailController',
    controllerAs: 'adc',
  }).
  otherwise({
    redirectTo: '/list'
  });
}]);

我的index.html

<!doctype html>
<html lang="en" ng-app="myApp">

<head>
    <meta charset="UTF-8">
    <title>Angular Demo</title>
    <script src="lib/angular/angular.min.js"></script>
    <script src="lib/angular/angular-route.min.js"></script>
    <script src="lib/angular/angular-animate.min.js"></script>

    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script> //include artistListController and artistDetailController
    <link rel="stylesheet" href="css/style.css">
</head>

<body>
    <div class="main" ng-view></div>
</body>

</html>

如果我使用angular-route.min.js v1.4.3,它会抛出$injector:unpr Unknown Provider并且网址会停留在http://127.0.0.1:56116/index.html

angular-route.min.js v1.2.10正常,网址为http://127.0.0.1:56116/index.html/#list

任何人都可以帮我这个吗?谢谢

2 个答案:

答案 0 :(得分:0)

此错误是由于$ injector无法解析所需的依赖项而导致的。要解决此问题,请确保已正确定义和拼写依赖项。 从artistControllers删除angular.module将解决问题。还在index.html文件中包含artistListControllerartistDetailController

答案 1 :(得分:0)

包括index.html中的所有控制器,如下所示:

<body>
    <script type="text/javascript" src="js/app.js"></script>
    <script type="text/javascript" src="js/controllers/artistListController.js"></script>
    <script type="text/javascript" src="js/controllers/artistDetailController.js"></script>
</body>

在你的app.js

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

myApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  when('/list', {
    templateUrl: 'includes/list.html',
    controller: 'artistListController'
  }).
  when('/detail/:id', {
    templateUrl: 'includes/detail.html',
    controller: 'artistDetailController'
  }).
  otherwise({
    redirectTo: '/list'
  });
}]);