小编典典

如何在angular js中附加到json对象

angularjs

我有这样的物体 $scope.releases = [{name : "All Stage",active:true}];

我需要附加更多数据

[
  {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


阅读 281

收藏
2020-07-04

共1个答案

小编典典

  $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}
  ]);
2020-07-04