Detect changed settings across controllers


#1

On the first page of my app I have two radio buttons, depending on which one is selected a particular list is displayed on the next view.

If I then return to the home page and change the radio button setting, when I move to the second view again, the list is not updated to reflect the change in setting from the radio button. (Changes to the radio button value is confirmed.)

It appears that the variable set from the radio button is not updated when the second controller/view is accessed the second and subsequent times.

I have tried:

  • using $rootScope to share the variable across the controllers
  • using localstorage to read the variable in the second controller

How do I set up the app such that when a variable is changed the controller can pick it up?

I figure there is probably a simple answer, but as a new learner to ionic/angular/js I can’t see it.

Thanks

John


#2

Why don’t you Google it.

This is probably the most asked AngularJS question:

https://objectpartners.com/2013/08/21/using-services-and-messages-to-share-data-between-controllers-in-angularjs/


#3

Thanks. Happy to share the very extensive browser history of my searches on the topic pre-posting. I had experimented with a number of what turned to out to be partial solutions (including trying to pass the variable in a service and using $watch). The difficulty when you are new to the field is tying all the pieces together to form a solution that works.

Your tutorial/example is extremely helpful as it ties the service, view and controller together with explanation. Many thanks for making resources such as this available to assist people such as myself in their learning.


#4

I’m glad to be of help :smile:


#5

To tie this off for anyone else interested, @Gajotres tutorial above is excellent. For my project, which uses tabs, I had to set cache = false to ensure my list was updated when the variable changed. (Without this the tab view was cached and the result was not updated when the variable was changes).

See also How do pass parameter on Tabs.