Saj*_*ith 12 javascript json append angularjs
我有这样的对象 $scope.releases = [{name : "All Stage",active:true}];
我需要向它添加更多数据
[
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]
Run Code Online (Sandbox Code Playgroud)
所以最终的数据应该是这样的
[
{name : "All Stage",active:true}
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码.但它没有附加.
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}
]
});
Run Code Online (Sandbox Code Playgroud)
Pluker Link:http://plnkr.co/edit/gist:3510140
net*_*eet 18
$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}
]);
Run Code Online (Sandbox Code Playgroud)
只需使用Array concat方法
$scope.release = [{name : "All Stage",active:true}];
$scope.releases = [
{name : "Development",active:false},
{name : "Production",active:false},
{name : "Staging",active:false}
];
$scope.releases = $scope.releases.concat($scope.release);
Run Code Online (Sandbox Code Playgroud)