簡體   English   中英

AngularJS-ng-select無法按預期工作

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

我有一些用於從本地存儲項讀取數據的表格數據的過濾器下拉菜單,下面顯示了select標簽,下面是將項目添加到select標簽和過濾器模型的代碼。

問題是,雖然刷新頁面時過濾的數據正確,但是如果本地存儲值為true,則所選項目僅顯示正確的值。

無論本地存儲項目為“ selected”還是“ selected”,始終將其添加到“從搜索中排除”選項。

無法為我的生活找出原因,感謝任何幫助建議。

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

您應該嘗試使用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