2014-02-12 27 views
1

我有一個下拉菜單,它將設置一個搜索模型對象用於ng-repeat過濾器。角爲什麼不清除我的過濾器?

<select ng-model="search.location_id" ng-options="id as name for (id,name) in search_locations"> 
    <option value="">Location</option> 
</select> 

我也有我的默認值'位置'來清除搜索。問題是,當我選擇一個位置(它正確過濾我的列表),然後返回到默認的「位置」選項,它會清除所有我的重複項目,而不是清除搜索過濾器。

<tr ng-repeat="course in courses | filter:search"> 

該課程對象顯然具有course.location_id屬性。 'search_locations'是通過表單{id:name,id:name}搜索的位置列表。

如何獲取我的完整列表? /清除這個特定的搜索參數?

回答

1

當角運行過濾器和值爲未定義則濾波器短路並返回整個列表(這是相同的行爲,如果在過濾器值爲前角1.2)

你可以做的是添加一個額外的對象到search_locations陣列設置爲未定義的LOCATION_ID:

$scope.search_locations = [ 
    { description: 'show all', location_id: undefined }, 
    { description: 'block 1', location_id: 1 }, 
    { description: 'block 2', location_id: 2 } 
]; 

演示fiddle

+0

很好的解決方法。 +1 :) –

0

這是因爲選擇默認選項時,分配給search.location_idnull如果search.location_id === null,則沒有在你的courses名單將匹配(當然,除非course.location_id === null)。

AngularJS docs上的示例顯示默認值爲null

被搜索的值時選擇的是什麼的實施例是nullhttp://plnkr.co/edit/meKe7l6rubaHEN1eRbzC?p=preview

模板

<body ng-controller="MainCtrl"> 
    <p ng-repeat="item in items | filter:search"> 
     Id: {{item.id}}, Name: {{item.name}} 
    </p> 
</body> 

控制器

app.controller('MainCtrl', function($scope) { 
    $scope.items = [{ 
    id: 1, 
    name: 'alice' 
    }, { 
    id: 2, 
    name: null 
    }]; 

    $scope.search = { 
    name: null 
    }; 
}); 

威爾僅輸出所述第二項目在items列表。

要解決此問題,請編寫您自己的filter函數。

+0

感謝您的迴應,但我仍然不知道如何讓我的整個重複列表回來。我覺得這應該是一件普通的事情。 – tommybananas

+1

@ snowman4415這是[long](https://github.com/angular/angular.js/issues/1780)[站立](https://github.com/angular/angular.js/issues/5117) [問題](https://github.com/angular/angular.js/issues/5055)和Angular,他們決定讓'null'匹配'null',而不是像'falsey'值。你可以編寫你自己的函數來以你自定義的方式過濾數組**或者**使用'ng-repeat'來創建你想要的'option',因爲'model'只使用'string'作爲'values'而不是物體。 –

相關問題