如何附加到角度js中的json对象 [英] How to append to a json object in angular js

查看:29
本文介绍了如何附加到角度js中的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我需要向它附加更多数据

<预><代码>[{名称:开发",活动:假},{名称:生产",活动:假},{名称:分期",活动:假}]

那么最终的数据应该是这样的

<预><代码>[{名称:所有阶段",活动:真}{名称:开发",活动:假},{名称:生产",活动:假},{名称:分期",活动:假}]

我尝试了以下代码.但它没有附加.

app.controller('MainCtrl', function($scope) {//我有一个这样的对象$scope.releases = [{name : "All Stage",active:true}];//我需要向它应用更多数据$scope.releases = [{名称:开发",活动:假},{名称:生产",活动:假},{名称:分期",活动:假}]});

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

解决方案

 $scope.releases = [{name : "All Stage",active:true}];//将新数组连接到原始数组上$scope.releases = $scope.releases.concat([{名称:开发",活动:假},{名称:生产",活动:假},{名称:分期",活动:假}]);

I am having an object like this $scope.releases = [{name : "All Stage",active:true}];

I need to append more data to it

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

So the final data should be like this

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

I tried the following code. But it is not appending.

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 Link : 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}
  ]);

这篇关于如何附加到角度js中的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆