【问题标题】:Access connect-flash messages in angular.js在 angular.js 中访问 c​​onnect-flash 消息
【发布时间】:2014-11-07 00:57:42
【问题描述】:

我正在修改一些预设传统非 SPA 页面加载的代码,并向用户显示由连接闪存中间件生成的闪存消息。它通过传入一个在 EJS 文件中呈现的对象来做到这一点。

我的应用程序是有角度的,我在后端没有模板引擎,并且希望避免使用一个。我怎样才能让角度意识到这些闪存消息?

这里是上下文: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
}));

感谢您的帮助!

【问题讨论】:

  • 两个想法,cookie 或 url 参数,如 'invalid',如果消息是微不足道的并且可以在 angular 中设置为简单的配置数组

标签: javascript angularjs node.js passport.js


【解决方案1】:

我的解决方案是创建一个 EJS 页面,其唯一目的是提取连接闪存数据并将浏览器重定向到指定的路由,并将数据附加为查询字符串。

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

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

});

情况可能会更糟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2012-06-09
    • 2010-09-11
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多