不能实例化AngularJS模块

Cannot instaniate module AngularJS

本文关键字:模块 AngularJS 实例化 不能      更新时间:2023-09-26

我的问题是,我的应用程序不能实例化我的模块,由于$routeProvider。

这是我的控制器:

var gameApp = angular.module("gameApp", []);
gameApp.controller("firstPageCtrl", function($scope,$http,$location) {
    $scope.doLogin = function() {
        $http.post("lib/action.php", {username: $scope.username, password: $scope.password}).success(function(data) {
            console.log(data);
        }).error(function(data) {
            console.log(data);
        });
    };
});
gameApp.config(function($routeProvider) {
    $routeProvider
    .when('/', {
            templateUrl : 'partials/firstpage.html',
            controller  : 'firstPageCtrl'
    })
    .when('/game', {
            templateUrl : 'partials/game.html',
            controller  : 'gameCtrl'
    });
});

gameApp.controller("gameCtrl", function($scope,$http,$location) {
});

我看不到代码中的错误。任何一个吗?

错误信息如下:

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

这是我的index.html文件:

<!DOCTYPE html>
<html ng-app="gameApp">
<head>
<script src="js/angular.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css">
<meta content="text/html;charset=UTF-8" http-equiv="content-type" />
<script src="js/mastercontroller.js"></script>
</head>
<body>
<div id="layout">
    <div id="topcontent">
    </div>
    <div id="middlecontent" ng-controller="firstPageCtrl">
        <div ng-view></div>
    </div>
    <div id="bottomcontent">
        {{"AngularJS"}}
    </div>
</div>
</body>
</html>

你需要把angular-route脚本加载到你的HTML页面中。

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

(此URL可能不适合您的情况,但您可以理解)。

然后你需要声明一个对ngRoute模块的依赖。

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

这是因为没有$routeProvider的提供者。

说一些像吼叫

gameApp.config(['$routeProvider',function($routeProvider) {
    $routeProvider
    .when('/', {
            templateUrl : 'partials/firstpage.html',
            controller  : 'firstPageCtrl'
    })
    .when('/game', {
            templateUrl : 'partials/game.html',
            controller  : 'gameCtrl'
    });
}]);