2016-06-14 70 views
0

我試圖使用角度服務從數據庫中刪除記錄。我在我的mvc控制器中創建了一個方法,但我不知道如何調用此方法並傳遞該ID。Angular從數據庫中刪除一條記錄

 
    [HttpPost] 
     public static void DeleteRecord(int settingID) 
     { 
      try 
      { 
       using (SqlConnection conn = new SqlConnection(connStringApps)) 
       { 
        conn.Open(); 
        using (SqlCommand command = new SqlCommand("DeleteCurrentRecord", conn)) 
        { 
         command.CommandType = System.Data.CommandType.StoredProcedure; 
         command.Parameters.Add("@SettingId", SqlDbType.VarChar).Value = settingID; 
         command.ExecuteNonQuery(); 
         command.Parameters.Clear(); 
        } 
        conn.Close(); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.Write(ex.ToString()); 
      } 

     } 


     myApp.service("deleteService", function ($rootScope) { 
      this.removeRow = function (recId) { 

      } 
     }); 


     myApp.controller('myController', ['$scope','deleteService', 
      function ($scope, deleteService) { 

       $scope.deleteRecordFromDB = function (recId) { 
        //record id is the Id of the record that I want to delete 
       } 
       ; 
      } 
     ]); 

回答

3
myApp.service("deleteService", function ($rootScope, $http) { 
     this.removeRow = function (recId) { 

     $http.delete(url, {params: {recordId:recId}}) 
     .success(function (data, status, headers, config) { 
      window.location.reload(); 
     }) 
     .error(function (data, status, header, config) { 
     }); 
     } 
    }); 

    myApp.controller('myController', ['$scope', 'deleteService', function ($scope, deleteService) { 
     $scope.deleteRecordFromDB = function (recId) { 
      deleteService.removeRow(recId); 
     }; 
    } 
    ]); 

您可以通過HTTPGET方法在服務器端訪問的recordId。

+0

謝謝。當我將服務添加到控制器時,是否需要執行其他任何操作?你能告訴我如何在服務器端使用httpGet訪問它。 – user6440175

+0

是的。你必須調用服務方法,$ scope.deleteRecordFromDB = function(recId){deleteId.removeRow(recId); } ; – rahulbhondave

+0

謝謝。我收到$ http沒有定義的錯誤。你能告訴我如何通過服務器端的httpGet訪問它嗎 – user6440175