简体   繁体   English

AngularJS-ng-select无法按预期工作

[英]AngularJS - ng-select not working as expected

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. 我有一些用于从本地存储项读取数据的表格数据的过滤器下拉菜单,下面显示了select标签,下面是将项目添加到select标签和过滤器模型的代码。

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. 问题是,虽然刷新页面时过滤的数据正确,但是如果本地存储值为true,则所选项目仅显示正确的值。

No matter what value the local storage item is selected="selected" is always added to the "Excluded from Search" option. 无论本地存储项目为“ selected”还是“ selected”,始终将其添加到“从搜索中排除”选项。

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',
    }];

You should try with the ng-options directive which IMO gives a simpler approach then ng-repeat 您应该尝试使用ng-options指令,IMO给出了比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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM