2014-03-27 56 views
3

我是新來的角, 在我的應用我有ng-repeat角JS與Startswith搜索自定義過濾器則包含了NG-重複

一個text box和列表,當用戶鍵入的文本框任何東西我正在通過使用以下代碼

<input type=textbox ng-model="TypedText"/> 

ng-repeat measure in newSearchResults |filter:TypedText 

但在角的缺省濾波器正在提供包含搜索過濾基於所鍵入的文本的 結果。 我需要startwith和next包含的結果。 我試圖編寫自定義過濾器,但沒有一個工作。幫助我創建自定義過濾器。

+1

已經answered..Please檢查這一點,它可能是有益 http://stackoverflow.com/questions/20179546/filter-ng-repeat-elements-from-the-beginning-of-the-string –

回答

1

您可以創建自己的自定義過濾器,如documentation所示。

{{ filter_expression | filter : array : expression : comparator}} 

您需要在此處提供一個功能,如expression

因此,讓我們假設您要根據值是否包含搜索文本進行過濾。您將在控制器中創建一個函數:

$scope.customFilter = function(value) { 
    // value parameter is the value passed in each ng-repeat 
    // In your example, it is the measure variable in "ng-repeat measure in 
    // newSearchResults 

    if (value.indexOf($scope.TypedText) !== -1) { 
     return true; 
    } else { 
     return false; 
    } 
}; 

如果在自定義過濾函數返回true,該值顯示否則它不是。

然後,您可以使用自定義過濾器就像任何其他過濾器:

<div ng-repeat="measure in newSearchResults | filter:customFilter()"> 
    //Repeating tags here 
</div> 
+0

調用過濾器'filter:customFilter()'時不要使用'()'。只需傳入名稱'filter:customFilter' – user1377911

0

就像在第一答覆中提到,你可以傳遞一個函數的過濾器,它不檢查你。

$scope.customFilter = function(item) { 

    var typedTextLength = ($scope.typedText) ? ($scope.typedText).length : 0, 
     substring = (item.yourValueToCheck).substring(0, typedTextLength); 

    return ($scope.typedText === substring); 

};