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.
- call function to change scope variable to = true
- call a different function later to check scope variable value
- 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.
this.scope.menu
is undefined and would error during the call toshowDetails
Are you able to share the full controller? – Brocco Sep 17 '15 at 15:08