如何在 ng-repeat (Angular JS) 的过滤器中使用数据绑定变量

How to use a data bound variable in a filter in ng-repeat (Angular JS)

本文关键字:过滤器 变量 数据绑定 ng-repeat JS Angular      更新时间:2023-09-26
<!-- Left Navbar -->
<div class="container-fluid" style="margin-top: 50px">
<div class="row">
    <div class="col-sm-3 col-md-2 sidebar">
        <ul ng-repeat="type in types" class="nav nav-sidebar">
            <li>{{type}}</li>                
            <li ng-repeat="sensor in sensors | filter: { type: {{type}} }"><a href="#">{{sensor.name}}</a></li>
        </ul>
    </div>
</div>

我正在尝试嵌套一个 ng 重复,并希望使用外部重复的值来确定内部重复。我想动态构建一个导航栏来列出对象 json 数组中的所有项目,并将它们列在其传感器类型(led、风扇、电机等(下,这可能吗?

我有一个示波器可变传感器,板上有所有传感器,还有一个类型数组,其中包含所有不同类型的传感器。

假设数据结构定义为

function Ctrl($scope) {
    $scope.types = [
        "A", "B"
    ];
    $scope.sensors = [{
        "type": "A",
            "name": "AA"
    }, {
        "type": "B",
            "name": "BB"
    }];
}

使用以下语法:

ng-repeat="sensor in sensors | filter: {'type': type}"