2014-03-27 57 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); 

};