json对象推送数据角度

时间:2014-02-20 11:57:19

标签: javascript api angularjs

我坚持在json对象api中添加数据。我可以使用push in object json添加新数据吗?它不会在json api中添加新对象

app.controller('InventoryCtrl', function($scope, $http) {   
    $http({method: 'GET', url: 'http://api.bos.lv/api/v1/inventory/?format=json'}).success( function(data){
        $scope.info = data; // response data 
    });

    $scope.add = function() {
        $scope.info.objects.push({"name":$scope.newinfo.name, "description":$scope.newinfo.description, "location":$scope.newinfo.location, "status":$scope.newinfo.status});
    };
    $scope.newinfo = {};
});

json文件,我从http

获取数据
{
  meta: {
  limit: 20,
  next: null,
  offset: 0,
  previous: null,
  total_count: 1
 },
  objects: [
      {
         count: 2,
          created: "2014-02-20T09:01:15.285510",
          description: "Camelion 2300mAh",
          id: 1,
          location: "IT nodala",
          name: "Baterija AA",
          published: true,
          resource_uri: "/api/v1/inventory/1",
          slug: "baterija-aa",
          status: "good condition"
      }
  ]
}

1 个答案:

答案 0 :(得分:0)

您只是在客户端进行更改。您必须发布/进行更改。当然,您必须在服务器上支持该请求。

$scope.add = function() {
    $scope.info.objects.push({"name":$scope.newinfo.name, "description":$scope.newinfo.description, "location":$scope.newinfo.location, "status":$scope.newinfo.status});

    $http.post('http://api.bos.lv/api/v1/inventory/', $scope.info).success(successCallback);
};

Angular $http

您可能希望使用单独的服务来执行这些HTTP请求。