0

everyone! I'm creating app with authorization. To store user data during session I use such service:

app.service('Session', function ($rootScope, $cookies) {

    var Session = $rootScope.$new();

    Session.createSession = function (accessTo, level, user_data) {

        this.level = level;
        this.accessTo = accessTo;

        if(user_data) {
            $cookies.isAuth = true;
        } else {
             $cookies.isAuth = false;
        }

        this.user = {
            email    : user_data.email,
            fullname : user_data.fullname,
            id       : user_data.id,
            level    : user_data.level
        }

        if(accessTo && accessTo.length == 1 && !$cookies.account) {
            this.account = accessTo[0].id;
            $cookies.account = accessTo[0].id;
        } else {
            this.account = $cookies.account;
        }
    };


    Session.destroy = function () {
        this.level = null;
        this.account = null;
        this.user = null;
        this.isAuth = false;
        $cookies.account = null;
    };

    return Session;
});

In controller use:

Session.createSession(some, params, here);

Afteer that it put some data to rootscope, and i can show it in console, but when i try to see for example Session.user/.level etc. it doesn't work. What is wrong?

5
  • Why are you storing your data on $rootScope? It's a service, so a singleton, so you can inject it in every controller you need and it will be the same instance. Commented Jul 22, 2015 at 10:39
  • Do you mean add something like: controllerTitle['$inject'] = ['Session']; What is other way to store data during session? Commented Jul 22, 2015 at 10:42
  • No, I mean app.service('Session', function ($rootScope, $cookies) { this.Session = whatever.... this would be a reference to the service itself which is a singleton. Commented Jul 22, 2015 at 10:47
  • emmm...i'll try. thanks. Commented Jul 22, 2015 at 10:48
  • Sorry, i think i don't understand how it should looks. Commented Jul 22, 2015 at 11:48

1 Answer 1

1

You should be doing this:

app.service('Session', function() {
    // A service is a singleton, as in it a persistent 
    // object you can inject in controllers
    this.foo = 'bar'; // anything on "this" is publicly available

    this.createSession = function() { ... };
    // you can also expose functions that you can call from the controllers 
});

app.controller('Ctrl1', function($scope, Session) {
    // You can inject "Session" here and use stuff you 
    // added on "this" inside of it

    $scope.foo = Session.foo;
    // you can put stuff on the controller's $scope
    // and you can bind to it in your HTML by using <span>{{foo}}</span>

    Session.foo = 'baz';
    // you can change stuff in the service and it will be persisted
});

So now if you navigate to a certain "Ctrl2" that injects "Session" too, Session.foo will be "baz", not the initial "bar" value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.