2015-10-15 24 views
0

我想用$ http delete從數據庫中刪除一個產品,但我不能,因爲我無法恢復表中元素的id。用angularjs刪除查詢

代碼: 丹斯樂controlleur:

@RequestMapping("/delete") 

public boolean deleteProduit(Long Id) 

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

丹斯html頁面:

<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> 

丹斯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); 
} 
}; 

拉頁JSON ES t sous cette forme

{「content」:[{「id」:5,「name」:「pk25」,「price」:125.0},{「id」:6,「name」:「 PK25" , 「價格」:125.0},{ 「ID」:7, 「名稱」: 「PK25」, 「價格」:125.0},{ 「ID」:8中, 「名稱」: 「PK25」, 「價格」 :125.0},{ 「ID」:9 「名稱」: 「PK25」, 「價格」:125.0}], 「總頁數」:2 「totalElements」:9 「最後」:假, 「尺寸」:5 , 「數字」:0, 「第一」:真正的 「排序」:空, 「numberOfElements」:5}

我需要幫助

+0

歡迎堆棧溢出。我編輯了你的帖子來解決英文問題和格式。 –

回答

0

您必須使用控制器@ResponseBody anotation。所以你的代碼就像這樣。 此代碼不完整。 春季控制器代碼

@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 ...'); 
      }); 
+0

問題出在這行$ http.delete('/ delete /'+ id) –

+0

這個id在undefined中。我如何恢復身份證 –