2016-06-14 89 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