角JS'Startswith“自定义过滤器自定义、过滤器、JS、Startswith

2023-09-13 03:15:52 作者:少女契约.

所以,我一直在努力了一段时间,使一个自定义过滤器中搜索'Startswith参数,而不是'包含'。我写每一个过滤器都似乎无法正常工作。下面是我想要实现---> http://jsfiddle.net/DMSChris/一个例子9ptr9 /

So I've been trying a while to make a custom filter that searches for the 'Startswith' parameters rather than the 'Contains'. Every filter that I've written haven't seem to work properly. Here is an example of what I'm trying to achieve ---> http://jsfiddle.net/DMSChris/9ptr9/

function FilterCtrl() {
var scope = this;
scope.doFilter = function(elem) { 
    if(!scope.searchText) return true;
    return elem.last_name.toLowerCase().indexOf( scope.searchText.toLowerCase()) == 0; 
};

}

http://jsbin.com/OyubElO/1/edit - 这里就是我在'现在米

http://jsbin.com/OyubElO/1/edit - Here is where I'm at right now.

  <ul border="1px" ng-repeat="msg in messages | filter:search:strict">

<li>{{msg.last_name}}</li>

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

推荐答案

一个简单的方法做,这是一种新的方法添加到您的范围。

A simple way to do this is to add a new method to your scope.

$scope.startsWith = function (actual, expected) {
    var lowerStr = (actual + "").toLowerCase();
    return lowerStr.indexOf(expected.toLowerCase()) === 0;
}

然后更换过滤器语法的元素。

Then change the filter syntax on your element.

<ul border="1px" ng-repeat="msg in messages | filter:search:startsWith">

下面是一个工作plunkr 上面的例子。

Here is a working plunkr with the example above.