1

I've made a really simple class,

user.class.js

'use strict';

class User {

constructor(id) {
    this.id = id;
}

getUserId() {
    return this.id;
}

}

and in my main controller, when someone logs in, I want to create a new instance of that class like this

const user = new User(2);

but how can I access this user from another controller?

Thank you

1
  • 2
    I would recommend you to use a service for such a logic. More precisely, create a global service stackoverflow.com/questions/14571714/… . Reference about what a service is: docs.angularjs.org/guide/services . PS: Avoid manually sharing variables through controllers, just use services since they are meant to do exactly such kind of activity. Making a global service allows you to have a two-way binded variable in each controller, which is controlled by the service. remember to make a GLOBAL service Commented Mar 29, 2017 at 9:13

1 Answer 1

1

For share variables in AngularJS between Controllers, you should to use services

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.