AngularJS中的ng-route不起作用

ng-route in AngularJS doesn't work

本文关键字:不起作用 ng-route 中的 AngularJS      更新时间:2023-09-26

我正在练习AngularJS,有一个问题:ng-route不工作,即使我添加了angular-route.js

这是我的app.js文件:

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

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl: 'views/intro.html'
    })
    .when('game', {
        templateUrl: 'views/game.html',
        controller: 'TableController'
    })
    .when('about', {
        templateUrl: 'views/about.html'
    })
    .otherwise({
        redirectTo: '/'
    });
}]);

这是我的index.html

<!DOCTYPE html>
<html>
<head>
    <title>Color Picking</title>
    <meta name="author" content="pexea12">
    <meta charset="utf-8">
    <meta name="viewport" content="device=device-width, initial-scale=1.0">
    <!-- CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">
    <!-- Script -->
    <script src="js/bootstrap/jquery-1.11.3.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
</head>
<body ng-app="app">
    <header>
        <br>
        <h2>Color Picking</h2>
        <br>
    </header>
    <div id="main">
       <div ng-view></div>
    </div> <!-- main -->
    <!-- AngularJS -->
    <script src="js/vendor/angular.min.js"></script>
    <script src="js/vendor/angular-route.js"></script>
    <script src="js/app.js"></script>
    <!-- Services -->
    <script src="js/services/ColorService.js"></script>
    <!-- Factories -->
    <script src="js/factories/RandomFactory.js"></script>
    <!-- Controllers -->
    <script src="js/controllers/TableController.js"></script>
</body>
</html>

我的文件夹树是:css (css文件,bootstrap)Js (app.js,控制器,服务,工厂,…)视图

我的网站工作与http://localhost:8080/,但不与localhost:8080/约或localhost:8080/游戏。

我真的被卡在这一点上,找不到解决办法。

我想你在你的之后有问题。当时,应该是:

$routeProvider
.when('/', {
    templateUrl: 'views/intro.html'
})
.when('/game', {
    templateUrl: 'views/game.html',
    controller: 'TableController'
})
.when('/about', {
    templateUrl: 'views/about.html'
})
.otherwise({
    redirectTo: '/'
});

你的路由中缺少了/