在NG-选项过滤器不工作 [英] Filter in ng-options not working

查看:137
本文介绍了在NG-选项过滤器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射的ID其对象的对象。我想显示这些对象的列表,并使用过滤器,但我不能得到它的工作。具体来说,我出现在这里列出的选项试图prevent对象ID为2是我得到了什么:
http://jsfiddle.net/9d2Za/

 < D​​IV NG-NG应用程序控制器=CTRL>
    过滤:
    <选择NG模型=selectedBidType
        NG-选项=bidType.id作为bidType.label为(bidTypeID,竞价方式)在bidTypes>    < /选择>
    < BR>
    过滤:
    <选择NG模型=selectedBidType
        NG-选项=bidType.id作为bidType.label为(bidTypeID,竞价方式)在bidTypes |过滤器:{ID:'2'}>    < /选择>
< / DIV>

请注意:我不能改变我们拿出任何修复程序的 bidTypes 对象的结构。这里是我的AngularJS:

 功能Ctrl($范围)
{
    $ scope.selectedBidType = 1;
    //出价类型目的通过用于快速查找的ID(这个不能在溶液中改变)索引
    $ scope.bidTypes = {
        1:{ID:1,标签:买入},
        2:{ID:2,标签:卖出},
        3:{ID:3,标签:缺少}
    };
}


解决方案

正如描述文档,在过滤器只能过滤接受一个数组作为第一个参数,而不是一个对象。在这种情况下,我个人使用自定义过滤器来进行转型:

  myModule.filter(
        objectToArray',
        [
            功能()
            {
                复位功能(对象)
                {
                    变种数组= [];
                    angular.forEach(对象,函数(元素)
                    {
                        的Array.push(元);
                    });                    返回数组;
                };
            }
        ]
    )
;

然后,在模板中

 <选择NG选项=...在bidTypes | objectToArray |过滤器:{ID:'2'}>

I have an object that maps IDs to their objects. I would like to display the list of these object and use a filter, but I cannot get it to work. Specifically, I'm trying to prevent object with ID 2 from appearing in the options Here's what I've got: http://jsfiddle.net/9d2Za/

<div ng-app ng-controller="ctrl">
    Unfiltered:
    <select ng-model="selectedBidType"
        ng-options="bidType.id as bidType.label for (bidTypeID, bidType) in bidTypes">

    </select>
    <br>
    Filtered:
    <select ng-model="selectedBidType"
        ng-options="bidType.id as bidType.label for (bidTypeID, bidType) in bidTypes | filter:{id: '!2'}">

    </select>
</div>

Please note: I cannot change the structure of the bidTypes object in whatever fix we come up with. Here's my AngularJS:

function ctrl($scope)
{
    $scope.selectedBidType = 1;
    // Bid type objects are indexed by ID for fast lookup (this cannot be changed in solution)
    $scope.bidTypes = {
        1: {id: 1, label: "Buy"},
        2: {id: 2, label: "Sell"},
        3: {id: 3, label: "Missing"}
    };
}

解决方案

As described by the documentation, the filter filter only accepts an array as first parameter, not an object. In such cases, I personally use a custom filter to make the transformation:

myModule.filter(
        'objectToArray',
        [
            function ()
            {
                return function (object)
                {
                    var array = [];
                    angular.forEach(object, function (element)
                    {
                        array.push(element);
                    });

                    return array;
                };
            }
        ]
    )
;

And then, in the template:

 <select ng-options="… in bidTypes | objectToArray | filter:{id:'!2'}">

这篇关于在NG-选项过滤器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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