ng-Route 不起作用

ng-Route doesn`t work

我的 ng-route 代码有问题。浏览器中的地址正在更改,但在我的页面上,所有元素仍然相同。这是我的代码:

var app=angular.module('mainApp',['ngRoute']);
app.config('$routeProvider', function($routeProvider){
  $routeProvider
      .when('/',{
            templateUrl: 'home.html'
      })
      .when('/about', {
            templateUrl: 'about.html'
      })
      .when('/contact', {
            templateUrl: 'contact.html'
      })
      .otherwise({ 
   redirectTo: '/'
   });
});

app.controller('mainCtrl',function($scope){

    });
<div ng-controller="mainCtrl">
 <div>
  <nav>
   <ul>
    <li><a href="#/">Home</a></li>
    <li><a href="#/about">About us</a></li>
    <li><a href="#/contact">Contact</a></li>
   </ul>
   </nav>
 </div>
   <br/>
 <div ng-view ></div>
</div>

我有三个带有简单段落的子网站(主页、关于和联系方式。html)。

你能帮帮我吗?

请使用app.config( function ( $routeProvider )代替app.config('$routeProvider', function ( $routeProvider )

脚本:

var app=angular.module('myApp',[]);
app.config(function($routeProvider){
  $routeProvider
      .when('/',{
            templateUrl: 'home.html'
      })
      .when('/about', {
            templateUrl: 'about.html'
      })
      .when('/contact', {
            templateUrl: 'contact.html'
      })
      .otherwise({ 
            redirectTo: '/'
            });
});

app.controller('mainCtrl',function($scope){

    });

Demo Fiddle : https://jsfiddle.net/jLdce3vj/43/

您在评论中提到 Desktop/SS/index.html#/ 是您的主页。 Therin 是你的问题。

除非您通过 HTTP 服务器提供页面,否则路由不起作用。不幸的是,在您的文件系统上静态查看您的 HTML 页面将无法正常工作。

如果您的系统上有 Python,您可以很容易地进行测试:打开命令提示符并切换到 Desktop/SS/ 目录。然后 运行 python -m SimpleHTTPServer 8080 并在浏览器中转到 http://localhost:8080。这样应该可以完美工作