如何在 ng-repeat 中按键分配对象数组?

How to assign arrays of objects by key in ng-repeat?

如何通过grid_id在angularng-repeat循环赋值elements到右边col并显示el内容?

我已经创建了 jsfiddle 工作示例。

<div ng-app>
    <div ng-controller="MainController">
        <div class="row" data-ng-repeat="row in rows">
            <div class="col" data-ng-repeat="col in row.col">{{col.grid_id}}</div>
            <!-- <div ng-repeat="el in elements track by col.grid_id">
              {{el.el}}
              <button ng-click="checkEl(row, col, $index)">check el</button>
            </div> -->
        </div>
    </div>
</div>

function MainController($scope) {
    $scope.elements = [{
        el: "Nutella",
        grid_id: 11
    }, {
        el: "Audi",
        grid_id: 4
    }, {
        el: "Samsung",
        grid_id: 7
    }];
    $scope.rows = [{
        col: [{
            grid_id: 1,
            elements: []
        }, {
            grid_id: 2,
            elements: []
        }, {
            grid_id: 3,
            elements: []
        }, {
            grid_id: 4,
            elements: [{el: "Audi"}]
        }]
    }, {
        col: [{
            grid_id: 5,
            elements: []
        }, {
            grid_id: 6,
            elements: []
        }, {
            grid_id: 7,
            elements: [{el: "Samsung"}]
        }]
    }, {
        col: [{
            grid_id: 9,
            elements: []
        }, {
            grid_id: 10,
            elements: []
        }, {
            grid_id: 11,
            elements: [{el: "Nutella"}]
        }, {
            grid_id: 12,
            elements: []
        }]
    }];
}
<div ng-repeat="el in elements | filter: {grid_id:col.grid_id} : true ">

这是你想要的吗? jsfiddle

想法是使用 filter 来匹配元素中的 grid_id。

track by 不适用于此。