如何从matInput检索值并通过HTTP请求发送?

时间:2018-06-19 05:33:05

标签: angular

我在Angular中创建了一个对话框 enter image description here

修改-dialog.component.html

<div id="edit-dialog">
  <table>
  <tbody>
  <tr>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="Nama profil" #input1>
     </mat-form-field>
  </div></td>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="MSISDN" #input2>
     </mat-form-field>
  </div></td>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="Paket aktif" #input3>
     </mat-form-field>
  </div></td>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="IMSI" #input4>
     </mat-form-field>
  </div></td>
  <td>  <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="ACC" #input5>
     </mat-form-field>
  </div></td>
  </tr>
  <tr>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="HPLMNwAcT" #input6>
     </mat-form-field>
  </div></td>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="OPLMNwAcT" #input7>
     </mat-form-field>
  </div></td>
  <td> <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="PLMNwAcT" #input8>
     </mat-form-field>
  </div></td>
  <td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="PLMNSel" #input9>
     </mat-form-field>
  </div></td>
  <td>  <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="OPL" #input10>
     </mat-form-field>
  </div></td>
  </tr>
  <tr>
  <td>  <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="PNN" #input11>
     </mat-form-field>
  </div></td>
  <td>  <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="SPN" #input12>
     </mat-form-field>
  </div></td>
  <td>  <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="KI" #input13>
     </mat-form-field>
  </div></td>
  <td>  <div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="OPC" #input14>
     </mat-form-field>
  </div></td>
  <td>&nbsp;</td>
  </tr>
  </tbody>
  </table>
  </div>

  <div class="mat-dialog-actions">
    <button [mat-dialog-close]=null cdkFocusInitials>Cancel</button>
    <button (click)="hello(#input1)" mat-dialog-close cdkFocusInitials>Update</button>
  </div>

修改-dialog.component.ts

import { Component, OnInit, Inject } from '@angular/core';
import { ApiService } from '../app.service';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';

@Component({
  selector: 'app-edit-dialog',
  templateUrl: './edit-dialog.component.html',
  styleUrls: ['./edit-dialog.component.css']
})

// export class EditDialogComponent implements OnInit {
export class EditDialogComponent {

  constructor(
    public dialogRef: MatDialogRef<EditDialogComponent>, private apiService: ApiService,
    @Inject(MAT_DIALOG_DATA) public data: any) { }

  onNoClick(): void {
    this.dialogRef.close();
  }

  hello(msg) {
    window.alert(msg);
  }
}

我想知道的是&#34;更新&#34;单击按钮,如何检索MSISDN值并通过HTTP请求发送?

后一部分将如下所示:

this.apiService.getData('update.php', 'msisdn').then(
      data => {
        // process the data here
      }
    );

app.service.ts

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

@Injectable()
export class ApiService {

  constructor(private http: Http) { }

  BASE_URL = 'http://localhost/esim-cms/';


    public getData(path: string, msisdn: string): Promise<any> {
      var addr = this.BASE_URL + path + "?msidn="+ msisdn;
      return this.http.get(addr).toPromise()
          .then((resp: Response) => {
              let data = resp.json();
              return data;
          });
      }
}

我还在弄清楚如何做前者

1 个答案:

答案 0 :(得分:2)

使用ngModel将输入与某个变量绑定,并使用这样的API将值发送到服务器 -

<td><div mat-dialog-content>
    <mat-form-field class="example-full-width">
      <input matInput placeholder="MSISDN" #input2 [(ngModel)]='msisdn'>
     </mat-form-field>
  </div></td>

this.apiService.getData('update.php', this.msisdn).then(
      data => {
        // process the data here
      }
    );

或者

如果要发送多个值,则可以使用Form来获取并向服务器发送多个值。