2016-07-18 15 views
1

Angular2アプリケーションでPHPコードを使用してMySqlデータベースからデータを削除するにはどうすればよいですか?最寄りのアドバイスは、角度1のためのものであり、以下の通りである。PHPのAngular2アプリでデータを削除

$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でどれ洞察力/経験をいただければ幸いです。

答えて

3

はい、http postはangular2と同様に動作します。投稿を使用したいので、リクエストに本文を追加したいと思います。

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

の詳細
関連する問題