2016-06-14 5 views
0

각도 서비스를 사용하여 데이터베이스에서 레코드를 삭제하려고합니다. 내 MVC 컨트롤러에서 메서드를 만들었지 만이 메서드를 호출하고 ID를 전달하는 방법을 모르겠습니다.각도 데이터베이스에서 레코드를 삭제하십시오.

 
    [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) { deleteService.removeRow (recId); 서비스 메서드를 호출해야합니다. } ; – rahulbhondave

+0

고맙습니다. $ http가 정의되지 않았다는 오류가 나타납니다. – user6440175

관련 문제