使用资源angularjs发布数据

时间:2014-02-27 14:32:34

标签: angularjs

我可以使用第一种方法发布数据,但是当我使用$ resource POST时,它不起作用。我的createInventory函数

出了什么问题
$scope.addInventory = function(inventory, $location) {
    $http.post("http://localhost/api/v1/inventory/", inventory)
        .success(function(data) {
         $scope.info.objects.key = data;
         $location.path('/inventory');
    });
};

使用这种方法它根本不起作用

$scope.createInventory = function(){
        Inventory.create($scope.info);
        $location.path('/inventory/add/');
};

这是模板

<label>Name</label>
    <input class="form-control" type="text" ng-model="inventory.name" /><br />
<label>Slug</label>
    <input class="form-control" type="text" ng-model="inventory.slug" /><br />
<label>Description</label>
    <input class="form-control" type="text" ng-model="inventory.description" /><br />
<label>Category</label>
<select ng-show="!show" ng-model="inventory.category" ng-options="category.name for category in category.objects">
     <option value="" selected>--Please select your category--</option> 
</select>
 <input type="button" class="btn btn-primary" ng-click="createInventory()" value="Save" />

我在其他模板中定义了ng-repeat

ng-repeat="inventory in info.objects"

如果需要,我的工厂

app.factory('Category', function($resource, $http) {
return $resource('http://localhost/api/v1/category/:id/', {},
    {
        update: {
            method: 'POST',
            isArray: false
        },
        save: {
            method: 'PUT'
        },
        query: {
            method: 'GET',
            isArray: false
        },
        create: {
            method: 'POST'
        },
        drop: {
            method: 'DELETE',
            params: {id: '@id'}
        }
    }
);
});

整个控制器

app.controller('InventoryCtrl', function($scope, $http, Inventory, Category, $location) {   

$scope.createInventory = function(){
    Inventory.create($scope.info);
    $location.path('/inventory/add/');
};

$scope.info = Inventory.query();

$scope.category = Category.query();

$scope.addInventory = function(inventory, $location) {
    $http.post("http://api.bos.lv/api/v1/inventory/", inventory)
      .success(function(data) {
        $scope.info.objects.key = data;
      });
};

});

我的api

    {
meta: {
limit: 20,
next: null,
offset: 0,
previous: null,
total_count: 2
},
objects: [
{
category: {},
count: 1,
created: "2014-02-27T16:23:40.813690",
description: "asd",
id: 50,
location: "asd",
name: "asd",
resource_uri: "/api/v1/inventory/50",
slug: "asd",
status: "Broken"
},
{
category: {},
count: 1,
created: "2014-02-27T16:46:05.017178",
description: "asd",
id: 51,
location: "sad",
name: "bubu",
resource_uri: "/api/v1/inventory/51",
slug: "bubu",
status: "Broken"
}
]
}

1 个答案:

答案 0 :(得分:0)

我猜你需要等到帖子完成才能移动到另一个位置......所以你需要改变回调的路径。

$scope.createInventory = function(){
    Inventory.create($scope.info, function(param) {
      $location.path('/inventory/add/');
    );
};