使用angularjs删除查询

时间:2015-10-15 10:10:23

标签: javascript angularjs

我想使用$ http delete删除数据库中的产品,但我无法恢复表格中元素的ID。

代码: dans le controlleur:

@RequestMapping("/delete")

public boolean deleteProduit(Long Id)

{
 produitRepository.delete(Id);
 return true;
}

Dans html page:

<td >{{p.id}}</td>
<td>{{p.name}}</td>
<td>{{p.price}}</td>
<td>
<a class='btn btn-danger' href='' ng-click='supprim(produits.content)'>
Delete
</a>

dans js:

$scope.supprim = function(data){
$scope.produits.content=data;

var index = $scope.produits.content.indexOf(data);
var result = confirm('Are you sure to delete this product ? '); 

if(result===true)
{
alert($scope.produits.content.valueOf(id))

$http.delete("/delete?Id="+$scope.produits.content.id);
                    $scope.produits.content.splice(index, 1);
}
};

La page json est sous cette forme

  

{&#34;内容&#34;:[{&#34; ID&#34;:5,&#34;名称&#34;:&#34; PK25&#34;&#34;价格&# 34;:125.0},{&#34; ID&#34;:6,&#34;名称&#34;:&#34; PK25&#34;&#34;价格&#34;:125.0},{& #34; ID&#34;:7,&#34;名称&#34;:&#34; PK25&#34;&#34;价格&#34;:125.0},{&#34; ID&#34 ;: 8,&#34;名称&#34;:&#34; PK25&#34;&#34;价格&#34;:125.0},{&#34; ID&#34;:9,&#34;名称&# 34;:&#34; PK25&#34;&#34;价格&#34;:125.0}],&#34;总页数&#34;:2&#34; totalElements&#34;:9,&#34 ;最后&#34;:假,&#34;大小&#34;:5,&#34;数&#34;:0,&#34;第一&#34;:真,&#34;排序&#34 ;:空,&#34; numberOfElements&#34;:5}

我需要帮助

1 个答案:

答案 0 :(得分:0)

您必须在控制器中使用@ResponseBody anotation。所以你的代码就像这样。 此代码不完整。 Spring控制器代码

   @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
   @ResponseBody
   public void delete(@PathVariable("id") Long id){    
   }

和你的angularjs代码就像这样

        var response = $http.delete('/delete/' + id)
          response.success(function (data, status, headers, config) {
          });
          response.error(function (data, status, headers, config) {
              alert('Error deleting ...');
           });