我正在学习使用angularjs的单页应用程序的示例。这是相关的代码:
index.html
<!DOCTYPE html>
<html ng-app="app">
<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.7.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.js"></script>
<script>
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
// configure the routes
$routeProvider
.when('/', {
// route for the home page
templateUrl: 'pages/home.html',
controller: 'homeController'
})
.when('/about/', {
// route for the about page
templateUrl: 'pages/about.html',
controller: 'aboutController'
})
.when('/contact/', {
// route for the contact page
templateUrl: 'pages/contact.html',
controller: 'contactController'
})
.otherwise({
// when all else fails
templateUrl: 'pages/routeNotFound.html',
controller: 'notFoundController'
});
});
app.controller('homeController', function ($scope) {
$scope.message = 'Welcome to my home page!';
});
app.controller('aboutController', function ($scope) {
$scope.message = 'Find out more about me.';
});
app.controller('contactController', function ($scope) {
$scope.message = 'Contact us!';
});
app.controller('notFoundController', function ($scope) {
$scope.message = 'There seems to be a problem finding the page you wanted';
$scope.attemptedPath = $location.path();
});
</script>
</head>
<body ng-controller="homeController">
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">My Website</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>
</header>
<div id="main">
<!-- this is where content will be injected -->
<div ng-view></div>
</div>
</body>
</html>
routeNotFound.html
<div class="jumbotron text-center">
<h1>This is not good</h1>
<p>{{message}}</p>
<p class="has-error">{{attemptedPath}}</p>
</div>
当我单击“主页”或“关于”或“联系方式”时,页面将正确显示。但是,如果我访问任何其他URL,则将routeNotFound.html正确注入div [ng-view],但数据未绑定。我得到:
This is not good
{{message}}
{{attemptedPath}}
当在路由内部调用.otherwise()时,似乎notFoundController无法正确提供给视图。 $ scope.message和$ scope.attemptedPath未绑定到视图。
您在notFoundController中缺少$ locationService
注入
app.controller('notFoundController', function ($scope,$location /*<--- location injected here*/) { $scope.message = 'There seems to be a problem finding the page you wanted'; $scope.attemptedPath = $location.path(); });
这里是完整样本:
var app = angular.module('app', ['ngRoute']); app.config(function ($routeProvider) { // configure the routes $routeProvider .when('/', { // route for the home page template: '<h1>My page home</h1><br />{{message}}', controller: 'homeController' }) .when('/about/', { // route for the about page template: '<h1>My page about</h1><br />{{message}}', controller: 'aboutController' }) .when('/contact/', { // route for the contact page template: '<h1>My page contact</h1><br />{{message}}', controller: 'contactController' }) .otherwise({ // when all else fails template: '<h1>Not found page</h1><br />{{message}}', controller: 'notFoundController' }); }); app.controller('homeController', function ($scope) { $scope.message = 'Welcome to my home page!'; }); app.controller('aboutController', function ($scope) { $scope.message = 'Find out more about me.'; }); app.controller('contactController', function ($scope) { $scope.message = 'Contact us!'; }); app.controller('notFoundController', function ($scope,$location) { $scope.message = 'There seems to be a problem finding the page you wanted'; $scope.attemptedPath = $location.path(); });
<!DOCTYPE html> <html ng-app="app"> <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.7.8/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.js"></script> <script> </script> </head> <body ng-controller="homeController"> <header> <nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="/">My Website</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> <li><a href="#!notsdflj"><i class="fa fa-comment"></i> Not found</a></li> </ul> </div> </nav> </header> <div id="main"> <!-- this is where content will be injected --> <div ng-view></div> </div> </body> </html>