我正在使用this code来使其与HTML5链接一起使用。不知何故,我无法使其工作:视图未解析为URL。另外,我不清楚我是否需要把" /"在视图名称前面。如果我这样做,浏览器URL链接会显示" file:/// addStudent" ,没有任何基础,另一方面,如果我没有,我会看到带有base的URL。这应该是那样的吗?在任何情况下,我都会看到错误消息:"找不到此网页" 。我在这做错了什么?谢谢!
testAngularJS.htm
<html>
<head>
<title>Angular JS Views</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
<base href="file:///home/myusername/projects/parser_ui/">
</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["$locationProvider", "$routeProvider", function($routeProvider, $locationProvider) {
// use the HTML5 History API
$locationProvider.html5Mode(true);
$routeProvider.
when('/addStudent', {
templateUrl: 'addStudent.htm',
controller: 'AddStudentController'
}).
when('/viewStudents', {
templateUrl: 'viewStudents.htm',
controller: 'ViewStudentsController'
}).
otherwise({
redirectTo: 'addStudent'
});
}];
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>
答案 0 :(得分:0)
在Plunkr中运行但不在本地运行:
<!DOCTYPE html>
<html>
<head>
<title>Angular JS Views</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
<base href="/">
</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(["$locationProvider", "$routeProvider", function($locationProvider, $routeProvider) {
// use the HTML5 History API
$locationProvider.html5Mode(true);
$routeProvider.
when('/addStudent', {
templateUrl: 'addStudent.htm',
controller: 'AddStudentController'
}).
when('/viewStudents', {
templateUrl: 'viewStudents.htm',
controller: 'ViewStudentsController'
}).
otherwise({
redirectTo: '/addStudent'
});
}]);
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>
&#13;
答案 1 :(得分:0)
在这一行(下面)我删除了括号,因为使用 file:/// 它给了我一个错误。由于SO的其他一些建议,我做到了。但是,括号必须在那里,否则,它将无法与服务器一起使用。
mainApp.config["$locationProvider", "$routeProvider", function($routeProvider, $locationProvider) {
......应该是:
mainApp.config(["$locationProvider", "$routeProvider", function($routeProvider, $locationProvider) {
例如,在nodejs中我添加了以下内容:
app.use('/*', function(req, res){
res.sendFile(path.resolve(__dirname + '/public/angular/views/index.html'));
});
话虽如此,当我评论上述情况时,它仍然运作良好,我不知道为什么。