Angular 使用 ng-view 的路由不工作

Angular routing with ng-view is not working

我不明白为什么我不能让它工作。

我会分享相关代码,如果您需要查看更多内容,请告诉我。

Index.html

<div class="col-md-3"><a href="#/liberals">Liberals</a></div>

app.js

var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.
    when("/liberals", {
        templateUrl: "partials/liberals.html"
        , controller: "LiberalsController"
    });
});

app.controller('LiberalsController', function ($scope, $http) {
    var url = "workingURL"; /// changed function to a simple string message to test
    $scope.message = "Hello Liberals";
});

(局部视图)liberals.html

    <h1>Hello</h1> 
     {{message}}

PS:我不是在支持或反对自由主义者的政治仇恨网站上工作!

从 AngularJS 1.6 开始,hashPrefix 的默认值已更改为 !

有两种方法可以让您的路由与 AngularJS 1.6+ 一起工作:

  • 将哈希前缀 (!) 添加到您的 href

<a href="#!/liberals">Liberals</a>

  • 使用 $locationProvider 更改(删除)hashPrefix 值:

$locationProvider.hashPrefix('');

我创建了一个工作 plunkr,其中我使用了第二种方法: https://plnkr.co/edit/oTB6OMNNe8kF5Drl75Wn?p=preview

可以找到有关此重大更改的提交 here