如何将会话variables从express / node传递给angular?

所以基本上我使用angular度路线,并使用快递来呈现索引页面。

我想要传递一个会话variables从expression到angular度,但我不知道如何获得每个angular度的路线中的variables。

app.js(快递):

app.get('*', function (req, res) { res.sendFile(__dirname+'/client/views/index.html',{ isAuthenticated:req.isAuthenticated(), user:req.user }); }); 

的index.html

 <body> <div class="main" ng-view> </div> </body> 

Angular应用程序文件:

 var myapp=angular.module('myApp', ['ngRoute','ngStorage']); myapp.controller('contrl1', ['$scope' ,'$localStorage','$http', function ($scope, $localStorage,$http) { }]); myapp.controller('contrl2', ['$scope' ,'$localStorage','$http', function ($scope, $localStorage,$http) { }]); myapp.config(function($routeProvider, $locationProvider) { $routeProvider .when('/try', { templateUrl: '../views/view1.html', controller: 'contrl1', }) .when('/hello', { templateUrl: '../views/view2.html', controller: 'contrl2', // configure html5 to get links working on jsfiddle $locationProvider.html5Mode(true); }); 

所以基本上我想发送Isauthenticatedvariables和用户variables,这将是具有会话值,从expression到angular.How应该继续这样的,这些值是可访问的angular度所有的路线,以及如何获得这些值在各自angular度控制器。

  app.get('*', function (req, res) { res.sendFile(__dirname+'/client/views/index.html',{ isAuthenticated:req.isAuthenticated(), user:req.user }); }); 

真的是一个坏主意。 有更好的方法来做到这一点。 这就是为什么你面临问题分享。 下面的方法将工作,请注意,我的解决scheme只是让你开始如何做这样的事情,使其安全,你可能不得不重构你的angularjs应用程序,做我在做什么,但在里面ng-service或ng-factory并将其注入到控制器中。 这样你就可以访问它。

考虑以下。

app.js

  var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'hbs'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); //This is very important, serve all your static files through public folder //Just inject your session variables to index.html, DO NOT PUT index.html //In you public folder, instead use index.hbs view as shown app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app; 

./routes/index.js

  var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'Pass session vairabled to my hbs', isAuthenticated: true, user: JSON.stringify({id: 2, name: 'dummy'}) }); }); module.exports = router; 

./views/layout.hbs

  <!DOCTYPE html> <html> <head> <title>{{title}}</title> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> {{{body}}} <!-- References of Angular.js and all your other angular files and services go here --> <script> var isAuthenticated= {{isAuthenticated}}; var user = {{{user}}}; </script> </body> </html> 

./views/index.hbs

  <h1>{{title}}</h1> <div class="main" ng-view> </div> 

以下是快照 – 概念validation。

在这里输入图像说明

现在在这里,我只是注入你的variables在普通的公共jsvariables,你需要编写ng-service / ng-factory来封装。

以下是我build议的结构

  . ├── LICENSE ├── README.md ├── app.js ├── bin │ └── www ├── package.json ├── public │ ├── images │ ├── javascripts │ ├── views │ │ └── <angular view templates> │ └── stylesheets │ └── style.css ├── routes │ └── index.js │ └── views ├── error.hbs ├── index.hbs //Your actual index file, this will contain ng-view └── layout.hbs 

希望这可以帮助!! 请让我知道,如果我可以有任何进一步的援助。

我不相信这是可能的。

即使使用sendFile ,只要设置了view engine ,仍然可以在文件中使用任何想要的模板。


你可以将它存储在一个可能隐藏的元素,然后只是使用js从它获取信息,或者我认为angular度使用类似的服务来加载这种types的数据后,你的控制器加载

 <input type="hidden" id="auth" value="#{authenticated}"/> 

然后只要使用JavaScript来获取控制器加载的值

但是,这是不吉利的,而不是真正的做事方式。 我很确定你应该使用这个服务。