Angular从数据库中删除记录

时间:2016-06-14 13:11:03

标签: angularjs

我正在尝试使用角度服务从数据库中删除记录。我在我的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
              }
              ;
          }
        ]);

1 个答案:

答案 0 :(得分: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。