简体   繁体   English

将JSON获取请求项推入数组

[英]push json get request items into an array

I'm trying to push json objects into an array with angular. 我正在尝试将json对象推入有角度的数组中。 I'm getting an error 'Cannot read property 'push' of undefined'. 我收到错误消息“无法读取未定义的属性“推””。 Is there another way to do this with angular? 有没有其他方法可以做到这一点?

$.each(data.Document.Placemark, function(index, item) {
   var locations = [];
  $scope.locations.push(item.name + ", " + item.Point.coordinates);
});

如果希望locations$scope的属性,则必须使其成为$scope的属性,而不是局部变量。

$scope.locations = [];

.push method is only used for arrays. .push方法仅用于数组。 You should define an array first before using .push You should do this: 在使用.push之前,您应该先定义一个数组。您应该这样做:

$.each(data.Document.Placemark, function(index, item) {
   $scope.locations = [];
   $scope.locations.push(item.name + ", " + item.Point.coordinates);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM