0

I have a setup with child controller calling a function in a parent controller. The method in the parent controller is called successfully, but when I go back to use the scope variable that function modified, the changes did not take.

ie.

  1. call function to change scope variable to = true
  2. call a different function later to check scope variable value
  3. scope variable still = false

First function call:

public showDetails(): void {
    this.scope.menu.showDetails = true; 
}

2cd function call:

// handle menu options
public selectMenuOption(type: string): boolean {
    if (this.scope.menu.showDetails) { // value is still false
        // DO STUFF
    }
}

Both of these functions reside in the same controller and access the same controller scope as such.

Besides the constructor initializing the value to false, it is not used anywhere else.

This is not about the view at this point, the variable in the controller is simply not updating. I know that to update the view you need to run $apply in certain scenarios, and I have tried using $apply on showDetails() on the off chance that also applied here. All I just got was an illegal access error which is what you get when running $apply during a $digest cycle.

I have discovered the culprit is going through the child controller to update the value in the parent controller. Even though the child controller calls the parent controller function fine, it doesnt seem to affect the parent controllers scope in this manner.

To fix this I will add this value to the service scope and update the parent scope in that manner. I was trying to avoid this extra step. Unless someone has a solution that doesnt involve the service.

3
  • Check your console for errors... based on what you have provided, this.scope.menu is undefined and would error during the call to showDetails Are you able to share the full controller? Commented Sep 17, 2015 at 15:08
  • I have scope.menu initialized in the controller as well Commented Sep 17, 2015 at 15:49
  • Without seeing the whole controller its kind of hard to help here. Short of that, I'd add some log statements at the start of each function to assure that they're being called. Commented Sep 17, 2015 at 19:06

1 Answer 1

0

From the question:

  • call function to change scope variable to = true
  • call a different function later to check scope variable value
  • scope variable still = false

JavaScript is not magic. So the only way this can happen is either :

  • The function isn't called when you think it. console.log is your best friend to debug this.
  • You have the wrong this. Try using an arrow function in all your member functions.
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.