Retaining select/input values when navigating back from detail page


#1

I’m having trouble retaining the selected values when navigating back to my list page from a detail page. I’m assuming it’s because I have a listctrl and a detailctrl that modifies the scope.

My areas and types in the service basically pull an object with various types {value:myvalue, name:myname}; I’m wondering if I should inject a value into my area/type when i submit a filtered result? so perhaps {value:myvalue, name:myname, selected:true} and then check that value in my ng-repeat and apply the selected state?

Or should I just set a variable somehow?

List View which I would like to retain values (or at least re-apply from the previous param/variable):

<select name="type" data-ng-model="param_type">
        <option value="">All Types</option>
        <option data-ng-repeat="type in types" value="{{type.value}}">{{type.name}}</option>
      </select>

      <select name="area" data-ng-model="param_area">
        <option value="">All Areas</option>
        <option data-ng-repeat="area in areas" value="{{area.value}}">{{area.name}}</option>
      </select>

      <button data-ng-click="filterResults()">submit</button>

Controller

.controller('ListCtrl', function($scope, $ionicLoading, ListService) {
  $scope.types = ListService.types();
  $scope.areas = ListService.areas();

$scope.filterResults = function() {
    var param_area = ""; 
    var param_type = ""; 

    if($scope.param_area != undefined){ param_area = $scope.param_area; }
    if($scope.param_type != undefined){ param_type = $scope.param_type; }

    console.log('clicked with {'+param_area+'} and {'+param_type+'}');
    $scope.places = ListService.filtered(param_area, param_type);
  };
}

#2

Services are the solution to this problem. Any time you want to retain information in an AngularJS app, you should use Services.

Using global variables is very un-Angular like.