我的AngularJS路线出了什么问题?

时间:2017-09-25 19:58:27

标签: javascript angularjs twitter-bootstrap ngroute

我已经创建了一个AngularJS沙箱,可以这么说。我使用Bootstrap提供的模板html来实现设置速度,我打算使用Bootstrap来快速实现它。在设置所有内容时,我注意到我的路由无法正常工作。在我的index.html文件中,我尝试将我的角度文件放在几个地方,但我不相信这是问题所在。我也尝试使用<ng-view></ng-view>,而不是使用div。在app.js中,我尝试将路由重命名为templateUrl,从根目录到结束文件app/home/home.html,以及许多其他配置。当我加载浏览器时,我得到的只是导航菜单。我甚至没有从templateUrl获取html。我可能错了所有这些,但我无法弄明白。我在我的index.html文件中放置了一个通用的Bootstrap导航,用于我的初始设置。可能是我的问题?

的index.html

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular-route.js"></script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
  </head>
  <body>

    <nav class="navbar navbar-expand-md navbar-dark bg-dark">
      <a class="navbar-brand" href="home.html">Angular</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
          <a class="nav-item nav-link" href="#">Home</a>
          <a class="nav-item nav-link" href="#">About</a>
          <a class="nav-item nav-link" href="#">Contact</a>
        </div>
      </div>
    </nav>



    <ng-view>
    </ng-view>



    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script src="app/app.js"></script>
    <script src="app/home/HomeController.js"></script>
  </body>
</html>

app.js

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

app.config(function($routeProvider){
  $routeProvider
    .when("/home", {
      templateURL: "home.html",
      controller: "HomeController"
    })
    .when("/about", {
      templateURL: "about.html",
      controller: "AboutController"
    })
    .otherwise({redirectTo: "/home"});
});

HomeController.js

var app = angular.module('myApp');

app.controller('HomeController', function($scope){

  $scope.noun = {
    person: "Maximus Aurelius",
    place: "World",
    thing: "sword"
  };

});

home.html的

<div>
  <h1>Hello {{noun.person}}</h1>
</div>

我的目录

enter image description here

开发工具网

enter image description here

2 个答案:

答案 0 :(得分:2)

模板的路径是相对于主页面的。您正在寻找页面文件所在的根目录中的模板,但它们位于子目录中:

尝试:

 $routeProvider
    .when("/home", {
      templateURL: "home/home.html",
      controller: "HomeController"
    })
    .when("/about", {
      templateURL: "about/about.html",
      controller: "AboutController"
    }) 

答案 1 :(得分:0)

看起来它只是你的$ routeProvider中的一个错字。 templateURL应该只是templateUrl。