3

I am new to AngularJS, I have a dropdown and a link. When I click the link, Anagularjs will route a different view (for example, display a chart and table).

Then when I click browser's back button, the dropdown will show the default value, other than the value I selected before.

Is it possible to let AngularJS remember the selected value of my dropdown when the link is clicked when I click browser's back button?

<select ng-model="selectedManagerFilter" ng-init="selectedManagerFilter= selectedManagerFilter || '*'" 
ng-options="item.Code as item.Name for item in ManagerFilters" id="lstManagementGroup" name="lstManagementGroup"></select>
1

1 Answer 1

3

Your scope will get cleared when you exit the page and recreated with the default values when you get back to it.

You have 2 options:

  1. use a Service to keep this kind of information (like selectedItem from your dropdown) and other useful things. The option selected in the dropdown should be bound to the service object:

    angular.module('shared').factory('UsefulService', function() {
    
    var UsefulService = {};
    
    UsefulService.myPageSettings = {
        currentDropDownItem: 1, //this is what you need
        otherSetting: "blah"
    };
    
    return UsefulService;
    });
    

and in your controller you should bind the scope variable to it (don't forget to require the UsefulService in your controller's dependencies):

$scope.myDearSettings = UsefulService.myPageSettings;

and then access it with $scope.myDearSettings.currentDropDownItem;

  1. you can set a hash on your route when the dropdown changes (bound to the value) so when you hit Back you will get to the same state because of the hash. Basically, the url in your address bar will look like: http://your_server:your_port/myPage#Today where Today is the selected item.

The most recommended solution is option #1.

Sign up to request clarification or add additional context in comments.

6 Comments

do you have an example of option 1?
thanks, but how can I bind this to my modal then? At the moment, the model is selectedManagerFilter. Should I change it to <select ng-model="myDearSettings.selectedManagerFilter" ...> ?
Hi, I changed it to a service, but got the same problem. What I can see is that when I hit back button, it will go back to the init setting code again and FilterSettingSvc.FilterSettings actually goes back to the default values as well.
Maybe you are setting the $scope value in the controller? This way it will always override the service setting when you get to that page. You should only rely on the data from the service.
No, can't really work this out at all. Do you have an example fiddle etc?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.