在restangular中获取名为post的属性

时间:2013-12-17 12:47:44

标签: javascript json angularjs post restangular

我正在使用以下代码获取“postrows”列表

$scope.postrows = {};
Restangular.all('/postrows').getList().then(function(data){
  $scope.postrows = data;
});

返回的JSON看起来像

{
  id: 1,
  post: {
    id: 1,
    postcategory: 1,
    account: "0005",
    description: "Grond",
    sequence_number: 1
  },
  amount: 2343.56,
},

但是当我循环通过项目列表时:

angular.forEach($scope.postrows, function(postrow) {
  console.log(postrow);
  console.log(postrow.post);
}

想要检索例如postrow.post我从restangular接收post函数。如何获取{j}文件中的post对象。

我不允许将名称post更改为其他内容。

1 个答案:

答案 0 :(得分:1)

Restangular实际上用Restangular对象覆盖你的post对象。

为此,只需将所有帖子方法更改为其他名称,如下所示:

Restangular.setRestangularFields({
  post: "$post"
});

从现在开始,post对象不会被覆盖,您可以Restangular.all('places').$post(newObject)创建一个新对象!