Angularjs 连接两个作用域数组

Angularjs Join two scope arrays

本文关键字:两个 作用域 数组 连接 Angularjs      更新时间:2023-09-26

如何在控制器的 angularjs 中将两个作用域数组连接成一个,以便我可以在单个表中显示它们?联接将发生在两个表中的字段上。这就像一个典型的 SQL 联接,您希望两个表的数据都放在一个表中。提前谢谢你。

$scope.stuff = [{desc: "stuff", id: "1234"},{desc: "things", id: "1235"}]
$scope.otherStuff = [{type: "new", id: "1234"},{type: "old", id: "1235"}]
$scope.result (how do I get this) = [{desc: "stuff", type: "new", id:"1234"},{desc: "things", type: "old", id:"1235"} ]

使用纯JavaScript:

$scope.result = $scope.stuff.concat($scope.otherStuff)

更新

正如艾米所说,你的数组是无效的。您似乎想用另一个对象扩展一个对象:

$scope.stuff = {desc: "stuff", id: "decbcf53-5d44-4d43-b1c4-5c3f83a129f8"};
$scope.otherStuff: = {type: "2", id: "decbcf53-5d44-4d43-b1c4-5c3f83a129f8"};
$scope.result: angular.extend($scope.stuff, $scope.otherStuff);

请注意,我使用{}而不是[]对象而不是数组。