【问题标题】:Splice from ng-options in select drop downusing filter使用过滤器在选择下拉菜单中从 ng-options 拼接
【发布时间】:2015-03-12 05:21:52
【问题描述】:

如何拼接数组中的一些元素以在ng选项中显示

这是 JSON

    $scope.result=[
            {
                "id": 1,
                "name": "min",

            },
            {
                "id": 2,
                "name": "hour",

            },
            {
                "id": 3,
                "name": "second",

            },
            {
                "id": 4,
                "name": "inch",

            },
            {
                "id": 5,
                "name": "km",

            }
    ]

我想从数组中拼接英寸和公里并显示它选择下拉菜单

<select ng-options="r.id as r.name for r in result"><option value="">--Select---</option></select>

还尝试过仅根据 id 显示的内联过滤器,例如 tihis <select ng-options="r.id as r.name for r in result|filter:{id:'!4'}"><option value="">--Select---</option> </select>

但这只会拼接一个元素,即 id:5(km)。我也想拼接 id:4

【问题讨论】:

    标签: javascript angularjs select filter


    【解决方案1】:

    为您的工作使用原生 javascript filter

    var newResult = $scope.result.filter(function(obj){
      return obj.name === 'km' || obj.name === 'inch';
    });
    console.log(newResult); // will give expected result.
    

    您可以将过滤器的结果分配给您要循环的scope中的键。

    【讨论】:

    • 谢谢@Mritunjay。解释得很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2015-01-09
    • 2021-07-03
    相关资源
    最近更新 更多