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

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

问题描述

所以我一直在尝试制作一个自定义过滤器来搜索Startswith"参数而不是Contains".我编写的每个过滤器似乎都无法正常工作.这是我试图实现的一个例子---> 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 - 我在这里正在.

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

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

任何帮助将不胜感激!

推荐答案

一个简单的方法是在你的作用域中添加一个新方法.

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.

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

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