0
我可以使用第一种方法发布数据,但是当我使用$ resource POST时,它不起作用。请告诉我错了我createInventory
功能使用资源张贴数据angularjs
$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"
}
]
}
你可以发布整个控制器吗? – Wawy
@Wawy增加新的信息。 – user3305175
你在开发工具中看到实际的请求吗? 为什么在第一个代码示例中使用'$ scope.info.objects.key = data;'赋值,但是在第二个代码示例中没有为$ resource指定任何值,如$ scope.info.objects。 key = Inventory.create()'? 而且一般情况下 - 你的意思是**'它不起作用'**? –