未捕获的错误: [$injector:modulerr] 由于以下原因,无法实例化模块 myApp: 错误: [$inj

Uncaught error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:unpr] Unknown provider: $routeProvider

本文关键字:错误 实例化 myApp inj 模块 injector modulerr      更新时间:2023-09-26

我收到此错误:

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>

我该如何解决?

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

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

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

<script src="scripts/angular/angular-route.js"></script>
如果尚未将此脚本下载到开发环境中

,则需要将其下载到开发环境中。

相关文章: