我正在改编一些以传统非 SPA 页面加载为前提的代码,并向用户显示由 connect-flash 中间件生成的 flash 消息。它通过传入在 EJS 文件中呈现的对象来实现此目的。
我的应用程序是 Angular ,我的后端没有模板引擎,并且希望避免使用模板引擎。我怎样才能让 Angular 意识到这些闪现消息?
上下文如下:passportjs 出错时重定向到/signup
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
感谢您的帮助!
最佳答案
我的解决方案是创建一个 EJS 页面,其唯一目的是提取 connect-flash 数据并将浏览器重定向到指定的路由,并将数据附加为查询字符串。
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('redirect.ejs', {redirect_url: "/?route=/signup&message="+req.flash('signupMessage')});
});
重定向.ejs
<html>
<meta http-equiv="refresh" content="0;<% if (redirect_url) { %><%=redirect_url %><% } %>" />
</html>
Angular Controller
app.controller("Index", function ($scope, $route, $routeParams,$location) {
var route= $routeParams.route, msg=$routeParams.message;
if (route) {
if ($location.$$search.route) {
delete $location.$$search.route;
$location.$$compose();
}
console.log(route, msg);
$location.path(route);
}
});
情况可能会更糟。
关于javascript - 访问 angular.js 中的 connect-flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25815187/