ng-route未加载其他页面的内容

时间:2017-01-20 06:21:24

标签: angularjs

的index.html

<!DOCTYPE html>
<html>
    <head>
        <title>MyProject</title>
        <!-- <script src="js/angular.min.js"></script> -->

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="css/mystyle.css" type="text/css">
        <!-- <script src="bootstrap-3.3.7-dist/js/jquery.min.js"></script> -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    </head>
    <body>
        <div ng-app="myApp">
            <header>
                <nav class="navbar navbar-inverse">
                    <div class="navbar-header">
                        <a class="navbar-brand" href="index.html"><img src="images/electronics.png" class="img-responsive"/></a>
                    </div>
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mynavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <div class="collapse navbar-collapse" id="mynavbar">
                        <ul class="nav navbar-nav">
                            <li><a href="#/">Home</a></li>
                            <!-- <li ng-class="{'active': methods.getActiveRoute()=='/about'}"><a href="#/about">About Us</a></li> -->
                            <li><a href="#about">About Us</a></li>
                            <li><a href="#contact">Contact US</a></li>
                        </ul> 
                    </div>
                </nav>
            </header>

            <section>
                <div ng-view>
                    home
                </div>
                <script>
            var app = angular.module("myApp", ["ngRoute"]);
            app.config(function($routeProvider) {
                $routeProvider
                .when("/", {
                templateUrl : "home.html"
                })
                .when("/about", {
                templateUrl : "about.html",
                })
                .when("/contact", {
                templateUrl : "contact.html"
                })
            });
        </script>
            </section>

            <footer>
              <div class="container-fluid foot">
               <div class="container">
                    <p style="color:black">CopyRight <span class="glyphicon glyphicon-copyright-mark"></span> All Rights Reserved</p>
               </div>
              </div>
            </footer>
        </div>
    </body>
</html>

home.html的

<div class="container" style="background-color:#7FFFD4">
    <h2><b>Hi!! This is home page</b></h1>
</div>

about.html

<div class="container" style="background-color:#FFEBCD" ng-controller="aboutController">
    <h1><b>About Us</b></h1>
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
        Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et 
        magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec,
        pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, 
        fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc</p>
    </div>

contact.html

<div class="container" style="background-color:#DEB887">
    <h1><b>Contact Us</b></h1>
    <pre>
        DieSachbearbeiter
        Choriner Straße 49
        10435 Berlin
        <b>E-Mail:</b> moinsen@blindtextgenerator.com
    </pre>
</div>

当我点击三个链接中的任何一个时,页面不会被定向到任何页面。它保持在同一页面上。

请调查它是否有任何错误..我先在这里发布了一个问题..请帮助.. !!!谢谢..

1 个答案:

答案 0 :(得分:0)

您应该在路线上添加斜杠。

<li><a href="#/about">About Us</a></li>
<li><a href="#/contact">Contact US</a></li>