使用$resource添加新项目
Add new item using $resource
我是AngularJS新手。学习它。我使用AngularJS的web API的GET
方法创建了产品列表。现在我想添加一个新产品。我正面临着几个问题。首先,我的ngDialog
是自动关闭每当我点击它外面,我不知道如何使用$resource
调用保存方法。这是我正在尝试的代码。
MyApp.controller('ProductController', function ($scope, $http, $resource, ngDialog) {
Product = $resource('/api/Product')
$scope.Product = Product.query();
$scope.OpenProductForm = function () {
ngDialog.open({ template: 'Product/AddNewTemplate.html' })
};
$scope.AddProduct = function () {
var NewProduct = $resource('/api/Product/:id', { id: '@id' });
NewProduct.save();
}
});
尝试将$resource服务路由添加到工厂中,并通过其方法在控制器中使用它:
MyApp.controller('ProductController', function ($scope, $http, yourResource, ngDialog) {
$scope.OpenProductForm = function () {
ngDialog.open({ template: 'Product/AddNewTemplate.html' })
};
$scope.AddProduct = yourResource.save({id: id}, function (response) {
$scope.id= response.id;
};
}).factory('yourResource', ['$resource', function ($resource) {
return $resource('/api/Product/:id', { id: '@id' } {
'save': {
method: 'PUT',
headers: {"Content-Type": "application/json"}
}
});
}]);
相关文章:
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- Redux处理新项目和id
- 使用方法将扫描的项目添加到总数中
- 将新对象添加到本地存储
- Ng-Repeat在末尾添加新项目
- 在 SharePoint 2010 上创建“添加新项目”按钮
- 如何将新项目添加到 srt 文件中
- 在SlickGrid的dataView的第一行添加一个新项目,就像JavaScript的unshift一样
- 当一个新项目被添加到FireBug控制台时,是否有一个jQuery事件等同于
- 如何使用javascript清除所有项目并在asp.net复选框列表中添加新项目
- 在knockout.js中添加具有多个属性的新项目
- 使用$resource添加新项目
- 光滑滑块-将新项目添加到中心
- 当新项目被添加到SharePoint日历时,正在运行Javascript函数
- 击倒多个下拉菜单,添加新项目
- 角和同位素-在同位素上下文中添加新项目
- jCarousel在末尾不断添加新项目
- 使用ENVDTE时的Java脚本-将新项目添加到现有解决方案中,并将其定位在特定的子文件夹中
- 为什么添加新项目时我的列表金额计数不会更新
- 我在尝试将新项目添加到 JSON 存储时收到“newItem() 未传递新项目的标识”错误