mysql — удаление данных в приложении Angular2 в переполнении стека

Как можно удалить данные из базы данных MySql, используя код PHP в приложении Angular2? Ближайший совет для Angular 1 и заключается в следующем:

$scope.deleteProduct = function(id){

// ask the user if he is sure to delete the record
if(confirm("Are you sure?")){
// post the id of product to be deleted
$http.post('delete_product.php', {
'id' : id
}).success(function (data, status, headers, config){

// tell the user product was deleted
Materialize.toast(data, 4000);

// refresh the list
$scope.getAll();
});
}
}

Можно ли использовать post метод аналогично:

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/Rx';

@Injectable()
export class HttpService {

constructor(private  http: Http) {}

deleteData() {
return this.http.post('delete_record.php')
}
}

Любое понимание / опыт работы с Angular2 / PHP будет приветствоваться.

1

Решение

Да, пост http работает аналогично в angular2. Поскольку вы хотите использовать post, я думаю, вы также хотите добавить тело к запросу.

import { Injectable } from 'angular/core';
import { Http } from 'angular/http';

@Injectable()
export class HttpService {

constructor(private  http: Http) {}

deleteData(data: SomeObject) {
let url = "delete_record.php";
let body = JSON.stringify(data);

return this.http.post(url, body)
.subscribe(
result => console.log(result),
error => console.error(error)
);
}
}

Вы также можете отправить запрос на удаление, что будет «наилучшей практикой».

 return this.http.delete(url)
.subscribe(
result => console.log(result),
error => console.error(error)
});

Подробнее о http-клиенте здесь https://angular.io/docs/ts/latest/guide/server-communication.html

3

Другие решения

Других решений пока нет …