0

I have a filter drop down for some tabular data that reads data from a local storage item, the select tag is shown below, and below that is the code to add items to the select tag and the model for the filter.

The issue is that whilst the data filtered is correct when you refresh the page, the selected item only shows the correct value if the local storage value is true.

No matter what value the local storage item is selected="selected" is always added to the "Excluded from Search" option.

Can't for the life of me work out why, any help advice appreciated.

<select class="form-control form-control-sm" ng-model="filterSearch" ng-change="setFilterS()" id="theFilterS" >                            
    <option ng-selected="{{option.value == filterSearch}}" ng-repeat="option in filterSearchOptions" value="{{option.value}}" >{{option.DisplayName}}</option>
</select>

$scope.filterSearch = localStorage.getItem("FilterSearch");

$scope.filterSearchOptions = [{
        value: '',
        DisplayName: 'All',
    }, {
        value: 'false',
        DisplayName: 'Included in Search',
    }, {
        value: 'true',
        DisplayName: 'Excluded from Search',
    }];

1 Answer 1

1

You should try with the ng-options directive which IMO gives a simpler approach then ng-repeat

angular.module('app',[]).controller('testCtrl',function($scope){
$scope.filterSearch = 'true';

$scope.filterSearchOptions = [{
        value: '',
        DisplayName: 'All',
    }, {
        value: 'false',
        DisplayName: 'Included in Search',
    }, {
        value: 'true',
        DisplayName: 'Excluded from Search',
    }];
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="testCtrl">
<select 
  class="form-control form-control-sm" 
  ng-model="filterSearch" 
  ng-change="setFilterS()" 
  id="theFilterS" 
  ng-options="option.value as option.DisplayName for option in filterSearchOptions">
</select>

{{filterSearch}}

</div>

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

2 Comments

Have tried this code and the values for each option are appended with string: eg. <option label="Included in Search" value="string:false">Included in Search</option>, and this is added - <option value="?" selected="selected"></option>
used str.split to get the boolean value and all workd perfectly, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.