如何通过另一个数组中的键显示数组中的属性

How to show proprty from array by keys from anther array

我正在尝试根据相同的键显示来自 2 个数组的数据, 这个例子是

第一个数组

[{$id='12321',name='test'},{$id='12312',name='test'},{$id='1234',name='test'}]

第二个数组:

[{$id='12321',value=4},{$id='12312',value=2}]

如何使用 ng-repeat 显示第一个数组,如果第二个数组有这个 id 来显示值。

我试过

 <div ng-repeat="qu in vm.qus">
                <div ng-repeat="max in vm.maxs | filter :{ max.$id === qu.$id } ">

错误:

Error: [$parse:syntax] Syntax Error: Token '.' is unexpected, expecting [}] at column 34 of the expression

你可以使用 ng-if,

 <div ng-repeat="qu in vm.qus">
    <div ng-repeat="max in vm.maxs" ng-if="max.$id ===qu.$id">
      {{max}}
    </div>
  </div>

演示

var app = angular.module('app', []);
app.controller('demoCtrl', ['$scope', function($scope) {
  vm = this;
  vm.qus = [{
    $id: '12321',
    value: 4
  }, {
    $id: '12312',
    value: 2
  }];

  vm.maxs = [{
    $id: '12321',
    value: 4
  }, {
    $id: '12312',
    value: 2
  }]
}]);
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title></title>
</head>

<body ng-app="app" ng-controller="demoCtrl as vm">
  <div ng-repeat="qu in vm.qus">
    <div ng-repeat="max in vm.maxs" ng-if="max.$id ===qu.$id">
      {{max}}
    </div>
  </div>
  <script type=" text/javascript " src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js "></script>
  <script type="text/javascript " src="MainViewController.js "></script>
</body>

</html>

你语法错误,应该是

<div ng-repeat="max in vm.maxs | filter :{ max: {$id :qu.$id } }: true as filtered">

虽然过滤集合的更好方法是控制器,因为它只会评估一次,其中,对 html 应用过滤本身在每个摘要周期都得到评估(在性能方面昂贵)

vm.filterd = $filter('filter')(vm.maxs, { max: {$id :qu.$id } }, true);

但是你必须再次考虑以确保你为每个 question.

申请以上