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.

share|improve this question
    
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? – Brocco Sep 17 '15 at 15:08
    
I have scope.menu initialized in the controller as well – appthat Sep 17 '15 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. – Brocco Sep 17 '15 at 19:06

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.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.