admin管理员组文章数量:1419666
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
3 Answers
Reset to default 4The Angular way would be using 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);
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
本文标签: javascriptAngularJS push two lists in oneStack Overflow
版权声明:本文标题:javascript - AngularJS push two lists in one - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745314710a2653097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论