【问题标题】:getelement by ID angular按 ID 角度获取元素
【发布时间】:2020-10-12 01:15:26
【问题描述】:

我使用 asp.net 和 angular 7,我尝试恢复由带有 get ID 的表单保存的数据,这些数据很好地存储在本地存储中,但它没有显示在用户的前端,也没有显示的错误你能帮我吗 在组件中我有这个代码

export class DashboardComponent implements OnInit {
  Syntheses: AddClient[];
  currentSynthese: AddClient;
  currentSyntheseSubscription:Subscription;
  public currentSyntheseSubject: BehaviorSubject<AddClient>;
  
getByIdSynthese(idSynthese:number){
  this.userService.getByIdSynthese(idSynthese).toPromise().then(res =>this.currentSynthese= res as AddClient);
}

在 HTML 中

<tr>
    <td>{{currentSynthese.Date}}</td>
    <td>{{currentSynthese.MCCD01}}</td>
    <td>{{currentSynthese.MCCD02}}</td>
    <td>{{currentSynthese.MCCD03}}</td>
    <td>{{currentSynthese.MCCD04}}</td>
    <td>{{currentSynthese.MCCD05}}</td>
    <td>{{currentSynthese.MCCI01}}</td>
    <td>{{currentSynthese.MCCI02}}</td>
    <td>{{currentSynthese.MCCI03}}</td>
    <td>{{currentSynthese.MCCI04}}</td>
    <td>{{currentSynthese.MCCI05}}</td>
    <td>{{currentSynthese.MACD01}}</td>
    <td>{{currentSynthese.MACD02}}</td>
    <td>{{currentSynthese.MACD03}}</td>
    <td>{{currentSynthese.MACD04}}</td>
    <td>{{currentSynthese.MACD05}}</td>
    <td>{{currentSynthese.MACD06}}</td>
    <td>{{currentSynthese.MACI01}}</td>
    <td>{{currentSynthese.MACI02}}</td>
    <td>{{currentSynthese.MACI03}}</td>
    <td>{{currentSynthese.MACI04}}</td>
    <td>{{currentSynthese.MACI05}}</td>
    <td>{{currentSynthese.MACI06}}</td>
    <td>{{currentSynthese.MOCD01}}</td>
    <td>{{currentSynthese.MOCD02}}</td>
    <td>{{currentSynthese.MOCD03}}</td>
    <td>{{currentSynthese.MOCI01}}</td>
    <td>{{currentSynthese.MOCI02}}</td>
    <td>{{currentSynthese.MOCI03}}</td>
</tr>    

并在服务中使用此代码

getByIdSynthese(idSynthese: number) {
    return this.http.get(this.rootURL+`/AddClients/${idSynthese}`);
} 

【问题讨论】:

  • 控制台日志资源,它给你什么?
  • 你在哪里给getByIdSynthese()打电话?
  • 没有错误信息或导致控制台日志
  • 我在组件中调用getbyIdSynthese

标签: html asp.net angular typescript angular7


【解决方案1】:

为什么不在你的服务中试试这个:

async getByIdSynthese(idSynthese: number): Promise<AddClient> {
    const result = await this.http.get(this.rootURL+`/AddClients/${idSynthese}`);
    console.log(result);
    return Promise.resolve(result);
}

在你的组件中:

async getByIdSynthese(idSynthese:number){
    this.currentSynthese = await this.userService.getByIdSynthese(idSynthese);
    console.log(this.currentSynthese);
}

首先,我认为它更简洁一些,然后,它可能只是避免混淆数据返回错误的位置。

【讨论】:

  • 你好,我使用了这段代码,但是如果你能帮助我,我不知道如何解决它。 src/app/_services/user.service.ts:35 中的错误: 9 - 错误 TS2740:“Observable”类型缺少“AddClient”类型中的以下属性:IdSynthese、Id、用户、日期等 37 个。 35 return Promise.resolve(result); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 这是一个类型错误。这意味着const result 未定义为 AddClient 类型。请添加如下类型const result: AddClient = etc....
猜你喜欢
  • 2020-09-20
  • 2016-02-25
  • 2010-12-11
  • 2011-03-25
  • 2017-06-14
  • 1970-01-01
  • 2019-02-04
  • 2019-09-11
  • 2018-07-14
相关资源
最近更新 更多