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

时间:2017-11-16 06:51:45

标签: javascript angularjs

我有一个过滤器下拉列表,其中包含一些从本地存储项读取数据的表格数据,下面显示了选择标记,下面是将项添加到select标记和过滤器模型的代码。 / p>

问题是,当您刷新页面时过滤的数据是正确的,如果本地存储值为真,则所选项目仅显示正确的值。

无论选择什么值,都选择本地存储项="选择"始终被添加到"从搜索中排除"选项。

不能为我的生活找出原因,任何帮助建议都会受到赞赏。

<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 个答案:

答案 0 :(得分:1)

您应该尝试使用ng-options指令,IMO提供了一种更简单的方法,然后是ng-repeat

&#13;
&#13;
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',
    }];
    });
&#13;
<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>
&#13;
&#13;
&#13;