使用Apache服务的AngularJS重新加载页面

时间:2015-06-03 14:48:03

标签: angularjs apache .htaccess

我有一个Angular应用程序,它使用了一些重定向。这是我的app.js的片段:

'use strict';

var cacheBustSuffix = Date.now();

angular.module('myApp', ['myApp.controllers', 'myApp.services', 'myApp.filters', 'ngRoute', 'ngResource', 'routeStyles'])
        .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
                $locationProvider
                        .html5Mode({enabled: true,
                            requireBase: false})
                        .hashPrefix('!');

                $routeProvider
                        .when('/', {
                            redirectTo: '/myApp'
                        }).
                        when('/myApp', {
                            templateUrl: '/partials/home.html?cache-bust=' + cacheBustSuffix,
                            controller: 'ctrlHome'
                        }).
                        when('/myApp/search', {
                            templateUrl: '/partials/search.html?cache-bust=' + cacheBustSuffix,
                            controller: 'ctrlSearch'
                        }).
                        otherwise({
                            templateUrl: '/partials/404.html?cache-bust=' + cacheBustSuffix,
                            controller: 'ctrl404'});
            }]);

整个事情由Apache 2.4提供。这是我的.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]

我可以手动并自动重新加载页面,但是当我这样做时,浏览器会重新读取必须刷新的部分,而不是我想要显示的索引+部分。因此,所有样式和脚本都无法加载,我放弃了界面和功能。我认为这可能是.htaccess文件的问题,但作为Apache的初学者,我对此并不十分自信。任何人都知道如何告诉服务器重新读取index.html和刷新视图?感谢。

1 个答案:

答案 0 :(得分:0)

我认为你只是缺少设置

    <base href="your/app/path">
在index.html中

如果你设置这个标签你会很高兴,也可以使用$ locationProvider打开你的html5模式,如$ locationProvider.html5Mode(true); 你的.htaccess工作正常。