GEt元素由id角7

问题描述 投票:0回答:1

你好,我需要检索一个表单输入的值后的post方法,所以我在服务级插入这段代码。

 list:Client[];


 constructor(private http:HttpClient) { }
 postClient(formData:Client){

  return this.http.post(this.rootURL+'/Clients/',formData);
 }
 putClient(formData:Client){

   return this.http.put(this.rootURL+'/Clients/'+formData.Engagement,formData);
  }
  getClient(formData:Client){

   return this.http.get(this.rootURL+'/Clients/GetClientByName/'+formData.Engagement);
  }

并在组件级这样的。

 getClient(form:NgForm){

  this.clientservice.getClient(form.value).subscribe(
    res =>{this.client = res as Client}
  )



 }

在HTML代码中,这个。

<table class="table table-hover">
                            <tr>

                                <th class="tname">Client</th>
                                <th class="tname">Enagement</th>
                                <th class="tname">ERP</th>



                            </tr>
                            <tr *ngFor="let clt of client">

                                <td >{{clt.Clientname}}</td>
                                <td >{{clt.Engagement}}</td>
                                <td >{{clt.ERP}}</td>

我不能通过ID获取值与get我不知道是什么问题,我既没有结果也没有错误信息。

html asp.net angular get angular7
1个回答
1
投票

我认为你的http service(this.rootURL+'ClientsGetClientByName)返回的是Client[]而不是Client.所以,你应该这样投。

  this.clientservice.getClient(form.value).subscribe(
    res =>{this.client = res as Client[]}
  )

但是,你的响应是固定的

 {"Engagement":"56789","Clientname":"ClLIENT","ERP":"ERP"}

在组件级,不需要编辑

 getClient(form:NgForm){

  this.clientservice.getClient(form.value).subscribe(
    res =>{this.client = res as Client}
  )



 }

你应该编辑html文件。

<table class="table table-hover">
                            <tr>

                                <th class="tname">Client</th>
                                <th class="tname">Enagement</th>
                                <th class="tname">ERP</th>



                            </tr>
                            <tr>
                                <td >{{client.Clientname}}</td>
                                <td >{{client.Engagement}}</td>
                                <td >{{client.ERP}}</td>
                            </tr>

---------------------------


或者需要使用ngFor循环,编辑组件级别,而不需要编辑组件级别。

 client: Client[] = [];
 getClient(form:NgForm){

  this.clientservice.getClient(form.value).subscribe(
    res =>{
            const cli = res as Client;
            this.client.length = 0;
            Array.prototype.push.apply(this.client, cli)
      }
  )



 }

© www.soinside.com 2019 - 2024. All rights reserved.