0

I'm using Angular new router components and also js. I have multiple controllers as the one bellow.

What I'm trying to do is to apply a function to this for all controllers, do I don't have to do it in each one.

function HomeController (authService, factoryClient) {
    console.log ('this is home controller');
    this = doCommonController.bind(this); //generates an error
    //here this should contain currentUser, authService, logout and testVar
    console.log(this);
}

and the function is:

var doCommonController = function (authService, currentUser) {
    this.testVar = 'value';
    this.authService = authService;
    this.currentUser =  currentUser;
    this.logout = this.authService.logout;
}

Also, how do I pass authService and factoryClient from controller to be available in doCommonController?

1 Answer 1

3

You need to use .call() not .bind(), also don't assign any value to this that is invalid

function HomeController(authService, factoryClient) {
    console.log('this is home controller');
    var currentUser;
    doCommonController.call(this, authService, currentUser); //generates an error
    //here this should contain currentUser, authService, logout and testVar
    console.log(this);
}
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.