最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS push two lists in one - Stack Overflow

programmeradmin1浏览0评论

I'm using angularJS and I have a problem: I have 2 $scope:

$scope.list1{
Object 1,
Object 2,
Object 3}

$scope.list2{
Object 4,
Object 5,
}

and I want this result:

$scope.res{
Object 1,
Object 2,
Object 3,
Object 4,
Object 5}

I don't know what function to use in agularJS, I did use

angular.merge($scope.res, $scope.list1, $scope.list2)

but I got empty result I don't know if merge is the good function so I need your help

I'm using angularJS and I have a problem: I have 2 $scope:

$scope.list1{
Object 1,
Object 2,
Object 3}

$scope.list2{
Object 4,
Object 5,
}

and I want this result:

$scope.res{
Object 1,
Object 2,
Object 3,
Object 4,
Object 5}

I don't know what function to use in agularJS, I did use

angular.merge($scope.res, $scope.list1, $scope.list2)

but I got empty result I don't know if merge is the good function so I need your help

Share Improve this question edited May 3, 2017 at 15:06 Mistalis 18.3k14 gold badges77 silver badges97 bronze badges asked May 3, 2017 at 15:00 Ragnar LodbrokRagnar Lodbrok 1631 gold badge3 silver badges18 bronze badges 3
  • 2 please add valid data. – Nina Scholz Commented May 3, 2017 at 15:01
  • 1 What nina is saying is that there is nothing in javascript with that syntax. There are arrays $scope.list = [obj1, obj2, obj3]; and there are objects $scope.list1 = { key: obj1, key: obj2 };. You are saying list, so arrays perhaps? But you are using curly brackets, so probably objects then? But there are no key/values so perhaps arrays anyway? As the question reads right now the answer you need is: Go and read some basic javascript tutorial that explains arrays and objects, and then please e back and try again! (Or you could edit your question and add valid data.) – ippi Commented May 3, 2017 at 15:21
  • look at this: stackoverflow./questions/43759566/… this is my problem, I didn't get any answer so I write a simple post here :) – Ragnar Lodbrok Commented May 3, 2017 at 16:30
Add a ment  | 

3 Answers 3

Reset to default 4

The Angular way would be using 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);

You can use array concat.

Ex:

$scope.res = $scope.list1.concat($scope.list2);
$scope.list1.forEach(function (selectedItem) {
        $scope.res.push(selectedItem);
    });
$scope.list2.forEach(function (selectedItem) {
        $scope.res.push(selectedItem);
    });

Try above code

发布评论

评论列表(0)

  1. 暂无评论