【问题标题】:Angular ngView not showing pageAngular ngView不显示页面
【发布时间】:2015-11-21 22:19:46
【问题描述】:

当我点击链接加载 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>

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

【问题讨论】:

    标签: javascript angularjs localhost ngroute ng-view


    【解决方案1】:

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

    /Pages/about.html

    应该变成

    页面/about.html

    【讨论】:

    • 谢谢!我实际上在发布这个问题后不久就解决了这个问题。现在 ngView 没有显示模板,我怀疑它与 localhost 相关
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2017-05-16
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多