我或者我应该使用Angularjs全局变量可以存储登录的用户? [英] Can I or Should I use a Global variable in Angularjs to store a logged in user?

查看:315
本文介绍了我或者我应该使用Angularjs全局变量可以存储登录的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度和发展我的第一个真正的应用程序。我试图建立一个日历/调度程序(来源$ C ​​$ C都可以在 github上),我希望能够改变内容,如果有登录与他们相关的),但在这里(即显示细节的用户的渔获:

I'm new to angular and developing my first 'real' application. I'm trying to build a calendar/scheduling app ( source code can all be seen on github ) and I want to be able to change the content if there is a user logged in (i.e. display details relevant to them) but here's the catch:


  1. 我不希望应用程序依赖于有一个用户登录(需要的东西,可以配置公开工作,私自或两者)

  1. I don't want the app to be dependent on having a logged in user ( needs to be something that can be configured to work publicly, privately or both)

我不想要实现这个应用中的用户/登录,如果能避免它(我想最终将包括我的应用程序在另一个应用程序在哪里,这可能会实现,但使用任何特定的不一定是实施安全框架或限制任何)

I don't want to implement the user/login within this app if it can be avoided ( I want to eventually include my app in another app where this might be implemented but isn't necessarily implemented using any particular security frameworks or limited to any)

我不得不创造一些全局变量的想法用户,可以通过被引用了我的申请,或者如果我不得不实施一个系统做这一切在此应用程序我可以在一些抽象的方式完成,这样不同的选择可在被注入。

I had an idea of creating some global variable user that could be referenced through out my application, or if I had to implement a system to do it all in this app that I could do so in in some abstract way so that different options could be injected in.

我的一些想法或者是我应该做的理解可能是完全错误的,无知的基本面,但我真的不知道我应该这样做什么办法。

some of my ideas or understanding of what I should be doing may be completely wrong and ignorant of fundamentals but I genuinely do not know what approach I should take to do this.

在情况下,它是相关的我目前还没有任何后端,但最终希望使​​用MongoDB的存储和对的NodeJS服务,但我也想试试把它不限成员名额,允许他人使用不同的存储/后端,比如如SQL和PHP

In case it is relevant I currently don't have any back-end but eventually hope use MongoDB for storage and nodejs for services but I also want to try keep it open-ended to allow others to use different storage/backends such as sql and php

有远有一个全球性的用户变量/服务,我可以注入/从另一个填充(父?)应用程序?

is there away to have a global uservariable/service that I could inject/populate from another (parent?) app?

如果这样,这将是这样做的最佳方法?
如果不是,为什么和我应该采取什么办法,为什么?

If so what would be the best approach to do so? If Not, why and what approach should I take and why?

我从网上的评论,对我提出的一项服务将是最好的选择但是一些建议相信如何我会去从父应用程序注入到这个应用程序的服务?

I Believe from comments online and some suggestion made to me that a service would be the best option BUT How would I go about injecting from a parent application into this applications service?

推荐答案

如果您的(单)页面是由服务器动态地提供,而该服务器知道,如果你登录或没有,那么你可以做以下内容:

If your (single) page is rendered dynamically by the server and the server knows if you are logged-in or not, then you could do the following:

动态呈现,产生一个脚本标记:

Dynamically render a script tag that produces:

<script>
     window.user = { id: 1234, name: 'User A', isLoggedIn: true };
</script>

对于非登录用户:

For non logged-in users:

<script>
     window.user = { isLoggedIn: false };
</script>

有关舒适,复制用户价值角度的IOC内部的:

For convinience, copy user to a value inside angular's IOC:

angular.module('myApp').value('user', window.user);

然后,您可以在DI使用它:

Then, you can use it in DI:

angular.module('myApp').factory('myService', function(user) {
    return {
        doSomething: function() {
            if (user.isLoggedIn) {
                ...
            } else {
                ...
            }
        }
    };
});

棘手的东西(你应该做的事情前两次[看评论])是的延长的的$范围:

angular.module('myApp').config(function($provide) {
    $provide.decorator('$controller', function($delegate, user) {
        return function(constructor, locals) {
            locals.$scope._user = user;
            return $delegate(constructor, locals);
        };
    });
});

这件code的装点 $控制器服务(负责contructing控制器)和基本上说, $范围对象传递给控制器​​之前,将与 _user 属性进行加强。

This piece of code decorates the $controller service (responsible for contructing controllers) and basically says that $scope objects prior to being passed to controllers, will be enhanced with the _user property.

有它自动$范围意味着你可以直接使用它的任何观点,任何地点:

Having it automatically $scoped means that you can directly use it any view, anywhere:

<div ng-if="_user.isLoggedIn">Content only for logged-in users</div>

这是危险的事,因为你可能会运行到命名冲突与原来的$范围API或属性您在控制器添加。

This is something risky since you may end up running into naming conflicts with the original $scope API or properties that you add in your controllers.

不言而喻,这些东西在客户端单独运行,他们可以很容易地篡改。你的服务器端code应经常检查用户并返​​回正确的数据集或接受正确的行动。

It goes without saying that these stuff run solely in the client and they can be easily tampered. Your server-side code should always check the user and return the correct data subset or accept the right actions.

这篇关于我或者我应该使用Angularjs全局变量可以存储登录的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆