注$范围为过滤器(AngularJS) [英] Inject $scope into filter (AngularJS)

查看:185
本文介绍了注$范围为过滤器(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的:

即时通讯使用的关联数组一个NG重复指令,我想过滤。在角过滤器的版本是不工作的关联数组/哈希。正因为如此,我试图写我自己的过滤器( http://angularjs.de启发/ ARTIKEL / angularjs-NG重复;它在德国,但重要的code是标题下方Beispiel 2:Filtern·冯·哈希mittels eigenem过滤器,这意味着示例2:过滤用自己的哈希过滤器)。

Im using a ng-repeat directive on an associative array, which I want to filter. The build in angular filter isn't working on associative arrays/hashes. Because of that, I'm trying to write my own filter (inspired by http://angularjs.de/artikel/angularjs-ng-repeat; it's in german, but the important code is below the headline "Beispiel 2: Filtern von Hashes mittels eigenem Filter" which means "Example 2: Filter Hashes with your own filter").

在NG-重复:

tr ng-repeat="(key, machine) in machines | customFilter | orderObjectBy:'name':false"

请注意:orderObjectBy也是customFilter

note: orderObjectBy is also a customFilter.

过滤器:

toolApp.filter('customFilter', [function(){
    return function(machines){
        var result = {};

        angular.forEach(machines, function(machine, key){
            /*if(machine.name.contains(filter)){
                result[key] = machine;
            }*/
            //if(machine.name.contains("a")){
                result[key] = machine;
            //}
        });
        return result;
    };
}]);

这个过滤器可以用硬codeD过滤器绕圈。但我想有列表上方的文本框,在其中,用户可以进入过滤器绕圈。 我的问题是,我不知道如何将这个值进入过滤器。我没有很多在互联网上,我发现了什么,并没有为我工作。

The filter works with a hardcoded "filter-criterium". But I want to have a textbox above the list, in which the user can enter the "filter-criterium". My Problem is, that i don't know how to insert this value into the filter. I don't find much on the Internet and what i found, didn't work for me.

有没有人一个想法如何插入值奥德也许不同的方法。

Has anyone an idea how to insert the value oder maybe a different approach.

在提前为每一个答案,非常非常感谢!如果您需要了解更多信息,让我知道!我试图保持简短:>

Huge thanks in advance for every answer! If you need more information, let me know! I tried to keep it short :>.

来自德国的问候

推荐答案

您可能必须参数,以您的自定义过滤器。

you may have parameters to your custom filter.

假设你有一个输入字段:

suppose you have an input field:

<input ng-model="myParam">

和NG-重复像您一样的:

and an ng-repeat like yours:

<tr ng-repeat="(key, machine) in machines | customFilter:myParam | orderObjectBy:'name':false" >

那么你可能在你的自定义过滤器访问该参数:

then you may access this parameter in your custom filter:

toolApp.filter('customFilter', [function(){
    return function(machines, myParam){
        var result = {};

        angular.forEach(machines, function(machine, key){
            if(machine.name.contains(myParam)){
                result[key] = machine;
            }
        });
        return result;
    };
}]);

请注意:
支付该myParam值与一个未定义值初始化关注。你必须在控制器中设置的默认值,或在customFilter处理它。否则,你只能看到你的清单,你开始打字了。

note: Pay attention that the myParam value is initialized with an "undefined" value. You have to set a default value in the controller or handle it in the customFilter. Otherwise you will only see your list, after you started typing.

这篇关于注$范围为过滤器(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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