gpt4 book ai didi

javascript - 如何 append 到 Angular js中的json对象

转载 作者:可可西里 更新时间:2023-11-01 01:59:15 25 4
gpt4 key购买 nike

我有一个这样的对象 $scope.releases = [{name : "All Stage",active:true}];

我需要向它 append 更多数据

[
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]

所以最终的数据应该是这样的

[
{name : "All Stage",active:true}
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]

我尝试了以下代码。但它不是追加。

app.controller('MainCtrl', function($scope) {
// I am having an object like this
$scope.releases = [{name : "All Stage",active:true}];
// I need to appned some more data to it
$scope.releases = [
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]
});

Pluker 链接:http://plnkr.co/edit/gist:3510140

最佳答案

  $scope.releases = [{name : "All Stage",active:true}];
// Concatenate the new array onto the original
$scope.releases = $scope.releases.concat([
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]);

关于javascript - 如何 append 到 Angular js中的json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20070790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com