将 $scope 注入过滤器(AngularJS) [英] Inject $scope into filter (AngularJS)

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

问题描述

我想做什么:

我在要过滤的关联数组上使用 ng-repeat 指令.内置角度过滤器不适用于关联数组/哈希.因此,我正在尝试编写自己的过滤器(灵感来自 http://angularjs.de/artikel/angularjs-ng-repeat;它是德语,但重要的代码在标题Beispiel 2:Filtern von Hashes mittels eigenem Filter"下面,意思是示例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-repeat:

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

注意:orderObjectBy 也是一个自定义过滤器.

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;
    };
}]);

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

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-repeat:

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.

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

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