I've set up routing on my project using AngularJS to load a different template when a menu button is clicked. The template is based on browser URL. Only problem is that the template will not load when the URL is visited directly. Instead it will load a blank page (object not found). How to overcome this?
当我点击菜单按钮时,我使用AngularJS在我的项目上设置路由以加载不同的模板。该模板基于浏览器URL。唯一的问题是,直接访问URL时不会加载模板。相反,它将加载一个空白页面(找不到对象)。怎么克服这个?
So the project is on local drive in folder /project4. There are three menu buttons, two of them linking to:
所以该项目位于文件夹/ project4中的本地驱动器上。有三个菜单按钮,其中两个链接到:
project4/about
and project4/contact
project4 / about和project4 / contact
This is the HTML section:
这是HTML部分:
<html>
<head>
<base href="/ordina/angular/project4/">
<link href=
"//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel=
"stylesheet">
<link href=
"//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel=
"stylesheet">
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<script src="script.js"></script>
<title></title>
</head>
<body>
<header>
<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>
</header>
<div id="main">
<div>
<p>This text is not being shown in the frontend.</p>
</div>
</div>
</body>
</html>
This is the .js file:
这是.js文件:
// script.js
// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);
// configure our routes
scotchApp.config(function ($logProvider, $routeProvider, $locationProvider) {
$logProvider.debugEnabled(true);
$locationProvider.html5Mode(true);
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController',
controllerAs: 'home'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController',
controllerAs: 'about'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController',
controllerAs: 'contact'
});
//$locationProvider.hashPrefix('!');
});
// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!';
});
scotchApp.controller('aboutController', function($scope) {
$scope.message = 'Look! I am an about page.';
});
scotchApp.controller('contactController', function($scope) {
$scope.message = 'Contact us! JK. This is just a demo.';
});
1 个解决方案
#1
4
You have html5mode turned on. In order to make the GET request for the URL work you have to implement a URL rewrite to the entry point of your AngularJS application first. (This is done Server Side in .htaccess)
你打开了html5mode。为了使URL的GET请求工作,您必须首先将URL重写到AngularJS应用程序的入口点。 (这是在.htaccess中完成的服务器端)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
Edit: htaccess in XAMPP
编辑:XAMPP中的htaccess
- Create htaccess.txt and paste content into it
- 创建htaccess.txt并将内容粘贴到其中
- On Windows do: Start Run > cmd
- 在Windows上执行:开始运行> cmd
- then
rename c:\pathtoyourhtaccessfile\htaccess.txt .htaccess
- 然后重命名c:\ pathtoyourhtaccessfile \ htaccess.txt .htaccess
- You might have to enable htaccess files in your httpd.conf (threw XAMPP Control Panel). Look for this Line:
AllowOverride All
It has to be set to "All", not "None" or sth else - 您可能必须在httpd.conf中启用htaccess文件(抛出XAMPP控制面板)。查找此行:AllowOverride All必须设置为“All”,而不是“None”或其他
- Restart Apache
- 重启Apache
#1
4
You have html5mode turned on. In order to make the GET request for the URL work you have to implement a URL rewrite to the entry point of your AngularJS application first. (This is done Server Side in .htaccess)
你打开了html5mode。为了使URL的GET请求工作,您必须首先将URL重写到AngularJS应用程序的入口点。 (这是在.htaccess中完成的服务器端)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
Edit: htaccess in XAMPP
编辑:XAMPP中的htaccess
- Create htaccess.txt and paste content into it
- 创建htaccess.txt并将内容粘贴到其中
- On Windows do: Start Run > cmd
- 在Windows上执行:开始运行> cmd
- then
rename c:\pathtoyourhtaccessfile\htaccess.txt .htaccess
- 然后重命名c:\ pathtoyourhtaccessfile \ htaccess.txt .htaccess
- You might have to enable htaccess files in your httpd.conf (threw XAMPP Control Panel). Look for this Line:
AllowOverride All
It has to be set to "All", not "None" or sth else - 您可能必须在httpd.conf中启用htaccess文件(抛出XAMPP控制面板)。查找此行:AllowOverride All必须设置为“All”,而不是“None”或其他
- Restart Apache
- 重启Apache