访问angular.js中的连接闪存信息

Access connect-flash messages in angular.js

本文关键字:连接 闪存 信息 angular js 访问      更新时间:2023-09-26

我正在修改一些代码,这些代码以传统的非SPA页面加载为前提,并向用户显示connect flash中间件生成的flash消息。它通过传递一个在EJS文件中呈现的对象来实现这一点。

我的应用程序是棱角分明的,后端没有模板引擎,我希望避免使用模板引擎。我如何让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页面,其唯一目的是提取连接闪存数据,并将浏览器重定向到指定的路由,数据附加为querystring。

// 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>

角度控制器

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);
    }
});

情况可能会更糟。