简体   繁体   English

如何从 Angular 中的 http/async 调用为接口属性赋值?

[英]How to assign value to interface property from http/async call in Angular?

I have a service that returns a JSON object and I want to assign this data to an interface property.我有一个返回 JSON object 的服务,我想将此数据分配给接口属性。 Here is the following code, the component.ts code here has been stripped down to contain only the relevant parts.这是下面的代码,这里的 component.ts 代码已被剥离,只包含相关部分。

Service.ts file服务.ts 文件

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  constructor(private httpClient: HttpClient) { }

  public getFanSpeed(){
    return this.httpClient.get('http://localhost:4000/auth/get-fan-speed');
  }
}

Component.ts file组件.ts 文件

import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../api.service';

interface CardSettings {
  content: string;
}

@Component({...})
export class DashboardComponent implements OnInit {
  fanSpeed: string;

  ngOnInit() {
    this.apiService.getFanSpeed().subscribe((response)=>{
      this.fanSpeed = response['fanSpeedVar'];
    });
  }

  fanSpeedCard: CardSettings = {
    content: this.fanSpeed
  };

  constructor(private apiService: ApiService) {}
}

I put a console.log inside the ngOnInit() function and I can see the correct value but for some reason it's just not getting assigned properly to the interface property, hence is just empty in the UI.我在 ngOnInit() function 中放置了一个 console.log,我可以看到正确的值,但由于某种原因,它没有正确分配给接口属性,因此在 UI 中只是空的。 Any guidance will be appreciated, thank you.任何指导将不胜感激,谢谢。

In your code the fanSpeedCard is a property that is assigned value of object (with CardSettings interface) at your class (DashboardComponent) construction time :在您的代码中,fanSpeedCard 是一个属性,在您的 class(DashboardComponent)构建时间分配了 object(带 CardSettings 接口)的值:

fanSpeedCard: CardSettings = {
    content: this.fanSpeed
};

Since fanSpeed is initially not defined (only type as string) and since you are not updating CardSettings object inside API response - your UI does not change.由于 fanSpeed 最初没有定义(仅键入为字符串)并且由于您没有在 API 响应中更新 CardSettings object - 您的 UI 不会改变。

As mentioned in the comment you need to make sure you update the value of the CardSettings' content property inside the subscribe block (not just fanSpeed):如评论中所述,您需要确保更新订阅块内的 CardSettings 内容属性的值(不仅仅是 fanSpeed):

gOnInit() {
    this.apiService.getFanSpeed().subscribe((response)=>{
      this.fanSpeed = response['fanSpeedVar'];
      this.fanSpeedCard.content = this.fanSpeed;
    });
}

You didn't correctly update fanSpeedCard 's content porperty value in ngOnInit() .您没有正确更新fanSpeedCard ()中 fanSpeedCard 的content属性值。 In ngOnInit() you reassigned the this.fanSpeed value.ngOnInit()中,您重新分配了this.fanSpeed值。 Here you should assign the service response value to fanSpeedCard 's content property.在这里,您应该将服务响应值分配给fanSpeedCardcontent属性。

The following solution will solve your issue.以下解决方案将解决您的问题。

Component.ts file组件.ts 文件

import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../api.service';

interface CardSettings {
  content: string;
}

@Component({...})
export class DashboardComponent implements OnInit {
  fanSpeed: string;

  ngOnInit() {
    this.apiService.getFanSpeed().subscribe((response)=>{
      this.fanSpeed = response['fanSpeedVar']
      this.fanSpeedCard.content = this.fanSpeed;
    });
  }

  fanSpeedCard: CardSettings = {
    content: this.fanSpeed
  };

  constructor(private apiService: ApiService) {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM