【发布时间】:2017-06-29 13:35:43
【问题描述】:
我在 AngularJS 中有一个简单的单页应用程序。 URL 看起来像这样.../Angular/index.html,当我点击页面中的链接时,它会转换为.../Angular/index.html#/addStudent。
现在,我想从 URL 中删除 #,但我无法做到。我用谷歌搜索并找到了很多答案,但没有一个有效(我对 Angular 和编程真的很陌生),所以可能是我错过了一些非常愚蠢的东西。
代码如下:
<html>
<head>
<title>Angular JS Views</title>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app = "mainApp">
<p><a href = "#addStudent">Add Student</a></p>
<p><a href = "#viewStudents">View Students</a></p>
<div ng-view></div>
<script type = "text/ng-template" id = "addStudent.htm">
<h2> Add Student </h2>
{{message}}
</script>
<script type = "text/ng-template" id = "viewStudents.htm">
<h2> View Students </h2>
{{message}}
</script>
</div>
<script>
var mainApp = angular.module("mainApp", ['ngRoute']);
mainApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/addStudent', {
//url:'/',
templateUrl: 'addStudent.htm',
controller: 'AddStudentController'
}).
when('/viewStudents', {
//url:'/',
templateUrl: 'viewStudents.htm',
controller: 'ViewStudentsController'
}).
otherwise({
redirectTo: '/addStudent'
});
//$urlRouterProvider.otherwise('/');
//$locationProvider.html5Mode(true);
//check browser support
// if(window.history && window.history.pushState){
//$locationProvider.html5Mode(true); will cause an error $location in HTML5 mode requires a tag to be present! Unless you set baseUrl tag after head tag like so: <head> <base href="/">
// to know more about setting base URL visit: https://docs.angularjs.org/error/$location/nobase
// if you don't wish to set base URL then use this
// $locationProvider.html5Mode({
// enabled: true,
// requireBase: false
// rewriteLinks: true
// });
// }
}]);
mainApp.controller('AddStudentController', function($scope) {
$scope.message = "This page will be used to display add student form";
});
mainApp.controller('ViewStudentsController', function($scope) {
$scope.message = "This page will be used to display all the students";
});
</script>
</body>
</html>
【问题讨论】:
-
@Ted 嗨,我知道这是重复的,但即使经过给定的解决方案,我也无法解决问题。
标签: javascript html angularjs angular-ui-router