将$ scope注入过滤器(AngularJS)

zoi*_*ois 2 inject angularjs angularjs-scope angularjs-filter

我正在做的事情:

我在关联数组上使用ng-repeat指令,我想过滤它.角度过滤器的构建不适用于关联数组/哈希.因此,我正在尝试编写自己的过滤器(灵感来自http://angularjs.de/artikel/angularjs-ng-repeat ;它是德语,但重要的代码低于标题"Beispiel 2:Filtern von Hashes mittels eigenem过滤器"表示"示例2:使用您自己的过滤器过滤哈希值").

ng-repeat:

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

注意:orderObjectBy也是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;
    };
}]);
Run Code Online (Sandbox Code Playgroud)

过滤器使用硬编码的"过滤器 - 标准".但是我想在列表上方有一个文本框,用户可以在其中输入"filter-criterium".我的问题是,我不知道如何将此值插入过滤器.我在互联网上找不到太多东西,我找到了什么,对我来说不起作用.

有谁知道如何插入值oder可能是一个不同的方法.

非常感谢您的每一个答案!如果您需要更多信息,请告诉我们!我试着保持简短:>.

来自德国的问候

mic*_*ael 6

您可能有自定义过滤器的参数.

假设您有一个输入字段:

<input ng-model="myParam">
Run Code Online (Sandbox Code Playgroud)

和你一样的ng-repeat:

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

然后您可以在自定义过滤器中访问此参数:

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;
    };
}]);
Run Code Online (Sandbox Code Playgroud)

注意:请注意myParam值是使用"未定义"值初始化的.您必须在控制器中设置默认值或在customFilter中处理它.否则,您只能在开始输入后看到列表.