在一个项目上工作,但这些路线似乎并不适合我。我试过了#!/'和'#'。下面是我的相同代码。我正在使用角1.6.4。 主页url是localhost / ang / index.htm,点击后链接url成为localhost / ang / index2.htm#about。必需的URL:localhost / ang / about
<!DOCTYPE html>
<html >
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<script >
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(["$routeProvider", "$locationProvider", function (e, r) {
r.hashPrefix('');
r.html5Mode(true);
e.when('/', { templateUrl : 'red.html', controller : 'mainController' })
.when('/about', { templateUrl : 'blue.html', controller : 'aboutController' })
.when('/contact', { templateUrl : 'green.html', controller : 'contactController' });
}]);
myApp.controller('mainController', function($scope) {
$scope.message = 'Test';
});
myApp.controller('aboutController', function($scope) {
$scope.message = 'Test';
});
myApp.controller('contactController', function($scope) {
$scope.message = 'test';
});
</script>
</head>
<body ng-controller="mainController" ng-app="myApp">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Angular Routing Example</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
</ul>
</div>
</nav>
<div id="main">
<div ng-view></div>
</div>
</body>
我在Stackoverflow上尝试了很多解决方案但不适合我。
答案 0 :(得分:1)
您的代码无法正常工作的原因是您没有为您的应用程序提及任何基本href。您可以删除r.html5Mode(true);
或添加基本href以使您的应用程序正常工作。但我建议您从localhost配置中删除r.html5Mode(true);
。在将代码部署到生产环境时,将其与基础href一起添加回来。
<强> HTML 强>:
<base href="/ang/">
<!-- Give a base Url for your application -->
<body ng-controller="mainController" ng-app="myApp">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header"> <a class="navbar-brand" href="/">Angular Routing Example</a> </div>
<ul class="nav navbar-nav navbar-right">
<li><a href="home"><i class="fa fa-home"></i> Home</a>
</li>
<li><a href="about"><i class="fa fa-shield"></i> About</a>
</li>
<li><a href="contact"><i class="fa fa-comment"></i> Contact</a>
</li>
</ul>
</div>
</nav>
<div id="main">
<div ng-view></div>
</div>
</body>
<强> JS 强>:
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(["$routeProvider", "$locationProvider", function(e, r) {
r.hashPrefix('');
r.html5Mode(true);
e.when('/home', {
templateUrl: 'red.html',
controller: 'mainController'
}).when('/about', {
templateUrl: 'blue.html',
controller: 'aboutController'
}).when('/contact', {
templateUrl: 'green.html',
controller: 'contactController'
});
}]);
myApp.controller('mainController', function($scope) {
$scope.message = 'Test';
});
myApp.controller('aboutController', function($scope) {
$scope.message = 'Test';
});
myApp.controller('contactController', function($scope) {
$scope.message = 'test';
});
Working Plunker :https://plnkr.co/edit/rPr4atjkuxnCwissmdl3?p=preview