本文介绍了为什么在此示例中,Angular js路由不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习angular js中的路由,请通过以下示例帮助我.在此示例中,路由不起作用.我需要在任何服务器上运行它吗?

I am learning routing in angular js, please help me with following example. Routing is not working in this example. Do I need to run this on any server?

<!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

main.html

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

red.html

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

green.html

green.html

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

blue.html

blue.html

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

推荐答案

尝试将URL用作

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

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

然后只有您的路线婴儿车设置有您的网址

Then only your route prams set with your URL

这篇关于为什么在此示例中,Angular js路由不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:10