为什么角度js路由不适用于此示例?

时间:2016-11-04 08:49:05

标签: javascript angularjs

我正在学习角度js中的路由,请帮我以下示例。此示例中的路由不起作用。我需要在任何服务器上运行吗?

<!DOCTYPE html>
<html>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>

  <body ng-app="myApp">

    <p><a href="#/">Main</a></p>

    <a href="#/red">Red</a>
    <a href="#/green">Green</a>
    <a href="#/blue">Blue</a>

    <div ng-view></div>

    <script>
        var app = angular.module("myApp", ["ngRoute"]);
        app.config(function($routeProvider) {
            $routeProvider
            .when("/", {
                templateUrl : "main.html"
            })
            .when("/red", {
            templateUrl : "red.html"
            })
            .when("/green", {
                templateUrl : "green.html"
            })
            .when("/blue", {
            templateUrl : "blue.html"
            });
        });
    </script>

    <p>Click on the links to navigate to "red.htm", "green.htm", "blue.htm", or back to "main.htm"</p>
</body>
</html>

main.html中

<h2> Hello this is main.html</h2>

red.html

<h2> Hello this is red.html</h2>

green.html

<h2> Hello this is green.html</h2>

blue.html

<h2> Hello this is blue.html</h2>

3 个答案:

答案 0 :(得分:0)

从href中删除#并尝试一次

<a href="/">Main</a>

答案 1 :(得分:0)

enter code here

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-route.min.js"></script>

<body ng-app="myApp">

  <p><a href="#/">Main</a>
  </p>

  <a href="#/red">Red</a>
  <a href="#/green">Green</a>
  <a href="#/blue">Blue</a>

  <div ng-view></div>

  <script>
    var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider) {
      $routeProvider
        .when("/", {
          templateUrl: "main.html"
        })
        .when("/red", {
          templateUrl: "red.html"
        })
        .when("/green", {
          templateUrl: "green.html"
        })
        .when("/blue", {
          templateUrl: "blue.html"
        });
    });
  </script>

  <p>Click on the links to navigate to "red.htm", "green.htm", "blue.htm", or back to "main.htm"</p>
</body>

</html>
&#13;
&#13;
&#13;

问题在于你的角度路线cdn。

答案 2 :(得分:0)

尝试将网址设为

<p><a href="/">Main</a></p>

<a href="/red">Red</a>
<a href="/green">Green</a>
<a href="/blue">Blue</a>

然后只有您的路线使用您的网址

设置