未捕获的错误:[$ injector:modulerr]由于以下原因无法实例化myApp模块:错误:[$ injector:unpr]未知提供者:$ routeProvider

时间:2016-01-26 18:56:11

标签: javascript angularjs module

我收到此错误:

ncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngRoute due to:
Error: [$injector:nomod] Module 'ngRoute' is not available! 
You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.4.9/$injector/nomod?p0=ngRoute

这是导致它的代码:

<!Doctype html>
<html data-ng-app="myApp">
<head>
    <title>Basic Angular</title>
</head>
<body>
    <!-- Placeholder for views -->
    <div ng-view="">
    </div>

    <script src="scripts/angular/angular.js"></script>

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

         myApp.config(['$routeProvider', function($routeProvider) {
              $routeProvider
                .when('/view1', {
                  templateUrl: 'views/view1.html',
                  controller: 'SimpleController'
                })
                .when('/view2', {
                  templateUrl: 'views/view2.html',
                  controller: 'SimpleController'
                })
                .   otherwise({
                  redirectTo: '/view1'
                });
         }]);

         var controllers = {}; 

         controllers.SimpleController = function( $scope ) {
            $scope.cars = [
                {brand:'Audi',  type:'SUV',         name:'Q5'},
                {brand:'Audi',  type:'Sedan',       name:'A7'},
                {brand:'Audi',  type:'Hatchback',   name:'Compact'},
                {brand:'BMW',   type:'SUV',         name:'X5'},
                {brand:'BMW',   type:'Sedan',       name:'X1'},
                {brand:'BMW',   type:'Hatchback',   name:'C3'}
            ];

            $scope.addCar = function () {
                $scope.cars.push(
                    {
                        brand : $scope.newCar.brand,
                        type : $scope.newCar.type,
                        name : $scope.newCar.name   
                    });
            };
        };

        myApp.controller(controllers);

    </script>
</body>

我该如何解决?

2 个答案:

答案 0 :(得分:2)

cdhowie的答案部分正确。下载角度路径库并将其链接到html后,您需要在角度模块中注入它的依赖性。 像这样......

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

答案 1 :(得分:0)

正如错误消息所述,该服务不可用。您没有包含提供此服务的脚本:

<script src="scripts/angular/angular-route.js"></script>

如果您还没有此脚本,则需要将此脚本下载到开发环境中。