0

我正在處理由元素的數據ID過濾的過濾器表達式。角度過濾器表達式

時遇到的問題是,該過濾器將返回多個值,如果其他ID的具有其它位置相同的位(即,過濾器12 RETURNS, 3, 34)。我想只是一個元素與12

JS的精確值返回

angular.module('app', []).controller('TestCtrl', function($scope) { 
$scope.customers = [ 
    { 
    id: 12, 
    name: 'customer 1 - 1' 
    }, 
    { 
    id: 123, 
    name: 'customer 2 - 12' 
    }, 
    { 
    id: 1234, 
    name: 'customer 3 - 12' 
    } 

]; 
}); 

HTML

Filter: <input ng-model="filterCustomer" /> 
<hr/> 
<select size="4" ng-options="customer as customer.name for customer in customers | filter:{id: filterCustomer} track by customer.id" ng-model="customerSelection"> 
</select> 

我也嘗試添加下列表達式過濾器匹配位長度......但如預期

filter:{id:filterFsaisCustomer.toString().length} 

Here是它不工作一個Plunker。

回答

1

應該通過增加true你的過濾器內部被用作嚴格的檢查,也應該有剛剛通過使輸入字段type="number"將解析到int轉換成integer值。

標記

<input type="number" ng-model="filterCustomer" /> 

<select size="4" 
    ng-options="customer as customer.name for customer in customers | filter:{id: filterCustomer}:true track by customer.id" 
    ng-model="customerSelection"> 
</select> 

Working Plunkr

編輯

要啓用過濾器後,纔在文本框中輸入值,那麼這將成爲有條件的基地嚴格檢查| filter:{id: filterCustomer}:filterCustomer>0

<select size="4" 
    ng-options="customer as customer.name for customer in customers | filter:{id: filterCustomer}:filterCustomer>0 track by customer.id" 
    ng-model="customerSelection"> 
</select> 

Updated Plunkr

+0

@ Kyle.Belanger檢查更新,plunkr –

+0

這個工作,但如果中斷值從輸入 –

+0

@ Kyle.Belanger清除看看更新plunkr及答案 –