角JS'Startswith“自定义过滤器 [英] Angular JS 'Startswith' custom filter

查看:154
本文介绍了角JS'Startswith“自定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在努力了一段时间,使一个自定义过滤器中搜索'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.

这篇关于角JS'Startswith“自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆