【问题标题】:GEt element By id angular 7获取元素按 id 角度 7
【发布时间】:2020-09-20 20:11:53
【问题描述】:

你好,我需要在 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 获取值我不知道是什么问题我既没有结果也没有错误消息

【问题讨论】:

    标签: html asp.net angular get angular7


    【解决方案1】:

    我认为您的 http 服务 (this.rootURL+'/Clients/GetClientByName/) 返回 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)
          }
      )
    
    
    
     }
    
    

    【讨论】:

    • 同样的事情我试图添加 [] 并且我没有结果
    • 你能看到 Chrome 调试器的服务响应吗?按 F12 按钮并单击调试器中的网络选项卡。并生成请求,您可以看到 Debugger 的响应。确认您的回复与您想要的一样。
    • 我已经测试了 POSTMAN 的 API 请求,并且我得到了我想要的响应,我认为问题在于以角度插入代码的级别,以便按 ID 生成数据跨度>
    • 您能看到预期的响应吗?
    • 我的预期响应是 [{"ClientName":"cli1", "Engagement":"eng1", "ERP":1},{"ClientName":"cli2", "Engagement": “eng2”,“ERP”:2}]。或 [{"ClientName":"cli1", "Engagement":"eng1", "ERP":1}] 如果您的预期响应是 {"ClientName":"cli1", "Engagement":"eng1", "ERP ":1},这不能做 ngFor 循环。改变你的反应。
    猜你喜欢
    • 2020-10-12
    • 2016-02-25
    • 2010-12-11
    • 2011-03-25
    • 2017-06-14
    • 1970-01-01
    • 2019-02-04
    • 2019-09-11
    • 2018-07-14
    相关资源
    最近更新 更多