Angular ngView不显示页面

时间:2015-11-21 22:19:47

标签: javascript angularjs localhost ngroute ng-view

当我点击链接加载ngView页面时,html文件不会显示。

我没有通过localhost运行应用程序,我怀疑这可能是问题所在。我刚刚用Chrome打开了index.html,所以也许ngRoute缺少一些依赖?


路由器应用/
--- index.html的
--- app.js
---页/
------- home.html的
------- about.html
------- contact.html

无论如何,这是我的app.js

// app.js

// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes
scotchApp.config(function($routeProvider){
    $routeProvider

        //route for home page
        .when('/', {
            templateURL: 'pages/home.html',
            controller: 'mainController'
        })

        //route for home page
        .when('/about', {
            templateURL: 'pages/about.html',
            controller: 'aboutController'
        })

        //route for home page
        .when('/contact', {
            templateURL: 'pages/contact.html',
            controller: 'contactController'
        })

        //default route
        .otherwise({
            redirectTo: '/'
        });
});

// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {

    // create a message to display in our view
    $scope.message = 'Everyone come and see how good I look!';
});

scotchApp.controller('aboutController', function($scope){
    $scope.message = 'Look! I am on the About page!';
});

scotchApp.controller('contactController', function($scope){
    $scope.message = 'Shoutout! I am on the Contact page!';
});

这是index.html

<!-- index.html -->
<!DOCTYPE html>
<html ng-app='scotchApp'>
<head>

  <BASE href="/Users/Khan/Desktop/router-app/">
  <!-- SCROLLS -->
  <!-- load bootstrap and fontawesome via CDN -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />

  <!-- SPELLS -->
  <!-- load angular and angular route via CDN -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
  <script src="script.js"></script>

  <meta charset='utf-8'>
  <base href='/'>
</head>
<body>

    <!-- HEADER AND NAVBAR -->
    <header>
        <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Angular Routing Example</a>
            </div>

            <ul class="nav navbar-nav navbar-right">
                <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
                <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
            </ul>
        </div>
        </nav>
    </header>

        <!-- angular templating -->
        <!-- this is where content will be injected -->
        <div ng-view></div>

</body>
</html>

如果有人知道的话,我很想知道发生了什么。谢谢您的帮助! :)

1 个答案:

答案 0 :(得分:1)

从模板网址中删除前导斜杠

/Pages/about.html

应该成为

Pages / about.html