AngularJS - 接入点/ EX preSS会话信息接入点、信息、AngularJS、preSS

2023-09-14 00:12:32 作者:阎王是我爹

我的工作,使用前preSS和护照进行身份验证和授权的节点应用,和所有的正常工作。我难倒,但是,我会如何获取当前用户(设定为通过护照req.user)在我的角度控制器。

I am working on a node application that uses Express and Passport for authentication and authorization, and all of that is working properly. I am stumped, however, on how I would get the current user (set as req.user by Passport) in my Angular controllers.

我希望能够做正常的事情就像在页眉/导航栏中显示的用户名(除其他事项外),和我目前做这个用的玉视图引擎,但我​​不喜欢用玉混合鉴于一些数据绑定的角度为他人引擎。

I want to be able to do normal things like displaying the username in a header/navigation bar (among other things), and I am currently doing this using the Jade view engine, but I don't like mixing using the Jade view engine for some "data-binding" and Angular for others.

现在,我知道我可以很容易地在节点的API端点返回req.user为JSON一个电话,但是,似乎是一个丑陋的解决方案,因为我会打端点每次有一个页面加载(这不是一个SPA)。

Now, I do know that I could easily make a call to an api endpoint in node that returns req.user as JSON, but that seems like an ugly solution, as I would be hitting that endpoint everytime there is a page load (this is not an SPA).

所以我的问题基本上是这样的:我可以得到当前登录的用户在我的角度的看法,所以我可以做我所有的数据绑定客户端,而不是一些服务器端(与玉视图引擎)和一些客户端与角?

So my question basically is this: Can I get the currently logged in user in my Angular views so I can do all my data-binding client side instead of some server side (with the Jade view engine) and some client side with Angular?

请注意:没有code段,因为一切实际工作为目的,我只是希望比我现在有一个更好的选择。

Note: No code snippets because everything actually is working as intended, I'm just hoping for a better alternative than what I currently have.

推荐答案

考虑使用NG-INIT上定义了NG-应用程序的DOM,即在Ex preSS打印服务器端数据(假设尘模板)。下面是一个简单的例子:

Consider using "ng-init" on the DOM that defines the ng-app, that is a server side data printed in Express (assume Dust template). Below is a simple example of this:

    <body ng-app="testApp" ng-controller="homeCtrl" ng-init="logged_in_user = {req.user}">
      <div...
      ...
    </body>

然后就可以访问LOGGED_IN_USER在homeCtrl,为scope.logged_in_user希望这有助于..

then you can access the logged_in_user in your homeCtrl, as "scope.logged_in_user" Hope this helps..

-Bhaskara

-Bhaskara