【发布时间】:2015-03-06 02:40:53
【问题描述】:
我有一个 angular.js 应用程序,其基本设置如下:我的 index.html 呈现静态内容,如顶部/侧边栏。在里面我有<div ng-view></div> 来显示部分html文件,例如我的home.html
登录到我的应用程序后,index.html 及其控制器 MainCtrl 以及我的 home.html 与相应的 HomeCtrl 一起加载。所以一切都应该是。
在我实施修复后(这样重新加载不会“忘记”用户并且我不会退出 - 我基于来自 http://www.sitepoint.com/implementing-authentication-angular-applications/ 的示例)我尝试重新加载我的页面。但现在只有我的 home.html 加载。
注意:我在下面编辑了我的发现
有道理,这是我app.js的一部分:
$routeProvider.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeCtrl',
resolve: {
auth: function ($q, authenticationSvc) {
var userInfo = authenticationSvc.getUserInfo();
if (userInfo) {
return $q.when(userInfo);
} else {
return $q.reject({ authenticated: false });
}
}
}
})
.when('/login', {
templateUrl: 'views/login.html',
controller: 'LoginCtrl',
login: true
})
很明显,当我加载/ 时,应用程序只显示我的home.html。但是如何实现,登录后正常进入页面时已经拥有的东西呢?
目前我的主要问题似乎是我不太确定登录时index.html 如何以及为什么加载正常。这是登录过程完成后我在LoginCtrl 中所做的事情:
$location.path("/");
我在网上搜索了有关此主题的一些详细说明,但遗憾的是我还没有找到任何有用的信息。有人可以向我解释为什么这首先有效,以及我如何使它也适用于页面重新加载?谢谢!
edit 我在这里 (https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode) 读到我必须更改我的快速配置。我试过了:
app.all('/*', function(req, res, next) {
res.sendfile('public/index.html', { root: __dirname });
});
我用$locationProvider.html5Mode(true);
但是当我现在登录时,我的页面会无限刷新直到崩溃。
edit2:好的,在阅读了很多关于 html5mode 以及如何将它与 express 一起使用之后,我很确定我的错误在后端,而不是在前端。
这是我的server.js(快速配置)的一部分
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'html');
app.set('views', __dirname + '/public/views/');
require('./routes')(app); // contains my other routes
app.get('/', function(req, res) {
res.render('index');
});
app.get('/views/:name', function (req, res) {
var name = req.params.name;
res.render('views/' + name);
});
app.get('*', function(req, res) {
res.redirect('/');
});
我的文件夹结构:
/server.js
/routes
/public/app.js
/public/index.html
/public/views/home.html
/public/controllers/xxxCtrl.js
我在 stackoverflow 和其他网站上读过很多帖子,基本上都这么说
app.get('*', function(req, res) {
res.redirect('/');
});
应该足以让这个工作,但对我来说似乎不起作用。
edit3: 快速总结一下我目前的问题是什么:
当我重新加载页面时,只有部分 html,例如home.html 正在加载中。不是index.html 的完整页面。这是我当前代码的快速摘要:
在我配置的app.js(角度配置)中:
$locationProvider.html5Mode(true);
// I tried my app without html5mode, but then everything breaks - but I need the html5mode anyway
$routeProvider.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeCtrl',
resolve: {
auth: function ($q, authenticationSvc) {
var userInfo = authenticationSvc.getUserInfo();
if (userInfo) { return $q.when(userInfo); }
else { return $q.reject({ authenticated: false });}
}
}
})
我的authenticationSvc:
.factory("authenticationSvc", ["$http", "$q", "$route", "$window", "$location", function ($http, $q, $route, $window, $location) {
// login, logout
function init() {
console.log("init");
if ($window.sessionStorage["userInfo"]) {
userInfo = JSON.parse($window.sessionStorage["userInfo"]);
}
}
init();
在服务器端,server.js:
app.get('/:name', function (req, res) {
var name = req.params.name;
res.sendFile('views/' + name);
// res.sendFile(__dirname + '/public/index.html'); // uncommented, since it didn't work
});
require('./routes')(app);
// this is my last route:
app.get('*', function (req, res) {
res.redirect('/#' + req.originalUrl);
});
据我了解,这“应该”足以让 express 和 angular 协同工作。
【问题讨论】:
-
经过这么多编辑,您能解释一下您目前面临的问题吗?
-
对
.htaccess文件有什么想法吗? -
我没有 .htaccess 文件,我使用 nodejs/express 和前端的 angular.js
标签: angularjs html node.js express route-provider