AngularJS 将两个列表合二为一
AngularJS push two lists in one
我正在使用 angularJS 但我遇到了问题:
我有 2 个 $scope:
$scope.list1{
Object 1,
Object 2,
Object 3}
$scope.list2{
Object 4,
Object 5,
}
我想要这个结果:
$scope.res{
Object 1,
Object 2,
Object 3,
Object 4,
Object 5}
不知道agularJS用什么函数,我用了
angular.merge($scope.res, $scope.list1, $scope.list2)
但我得到的结果是空的
不知道 merge 是不是好用的功能所以需要大家的帮助
您可以使用数组连接。
例如:
$scope.res = $scope.list1.concat($scope.list2);
Angular 方式 将使用 angular.extend
:
Extends the destination object dst
by copying own enumerable
properties from the src
object(s) to dst
. You can specify multiple src
objects. If you want to preserve original objects, you can do so by
passing an empty object as the target:
var object = angular.extend({}, object1, object2)
.
angular.extend($scope.list1, $scope.list2);
$scope.list1.forEach(function (selectedItem) {
$scope.res.push(selectedItem);
});
$scope.list2.forEach(function (selectedItem) {
$scope.res.push(selectedItem);
});
试试上面的代码
我正在使用 angularJS 但我遇到了问题: 我有 2 个 $scope:
$scope.list1{
Object 1,
Object 2,
Object 3}
$scope.list2{
Object 4,
Object 5,
}
我想要这个结果:
$scope.res{
Object 1,
Object 2,
Object 3,
Object 4,
Object 5}
不知道agularJS用什么函数,我用了
angular.merge($scope.res, $scope.list1, $scope.list2)
但我得到的结果是空的 不知道 merge 是不是好用的功能所以需要大家的帮助
您可以使用数组连接。
例如:
$scope.res = $scope.list1.concat($scope.list2);
Angular 方式 将使用 angular.extend
:
Extends the destination object
dst
by copying own enumerable properties from thesrc
object(s) todst
. You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target:var object = angular.extend({}, object1, object2)
.
angular.extend($scope.list1, $scope.list2);
$scope.list1.forEach(function (selectedItem) {
$scope.res.push(selectedItem);
});
$scope.list2.forEach(function (selectedItem) {
$scope.res.push(selectedItem);
});
试试上面的代码