我不明白为什么我不能让它工作。
我将分享相关代码,如果您需要查看更多内容,请告诉我。
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";
});
(部分 View )liberals.html
<h1>Hello</h1>
{{message}}
PS:我不是在支持或反对自由主义者的政治仇恨网站上工作!
最佳答案
从 AngularJS 1.6 开始,默认值 hashPrefix
已更改为!
.
有两种方法可以让你的路由与 AngularJS 1.6+ 一起工作:
- 将哈希前缀 (
!
) 添加到您的href
中的:
<a href="#!/liberals">Liberals</a>
- 更改(删除)
hashPrefix
值使用$locationProvider
:
$locationProvider.hashPrefix('');
我创建了一个工作 plunkr,其中使用了第二种方法: https://plnkr.co/edit/oTB6OMNNe8kF5Drl75Wn?p=preview
有关此重大更改的提交可以找到 here
关于angularjs - 使用 ng-view 的 Angular 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42816212/