简体   繁体   English

将JSON http响应绑定到Angular 6组件

[英]Bind JSON http response to Angular 6 components

I'm getting below JSON response from my HttpClient GET method 我从我的HttpClient GET方法获得JSON响应以下

{
  shortDescription: "3", 
  promotionName: "2", 
  highResolutionImage: "4", 
  lowResolutionImage: "5", 
  promotionOrChallengeCode: "aaa"
}

Promotion.ts Promotion.ts

export interface IPromotion
{

    PromotionOrChallengeCode:string;
    PromotionName:string;
    ShortDescription:string;
    HighResolutionImage:string;
    LowResolutionImage:string;
}

In my Component class 在我的Component类中

promotion:IPromotion;

onSubmit() : void {

    this.httpClient.get('http://localhost:8080/api/search/'+this.pcode )
    .subscribe((response:IPromotion) => 
          { 
            console.log(response);
            this.promotion = response; 
            console.log(this.promotion.PromotionOrChallengeCode);
          });
    }

In the Browser console, I'm able to view the JSON response (Output of first console statement). 在浏览器控制台中,我可以查看JSON响应(第一个控制台语句的输出)。 And the output of second console statement is displayed as "Undefined" 并且第二个控制台语句的输出显示为“未定义”

Let me know how to read JSON data and bind to HTML elements 让我知道如何读取JSON数据并绑定到HTML元素

Below are the current Angular versions I'm using: 以下是我正在使用的当前Angular版本:

C:\Users\893108>ng -v

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / ? \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 6.1.2
Node: 8.11.3
OS: win32 ia32
Angular:
...

Package                      Version
------------------------------------------------------

rxjs                         6.2.2

typescript                   2.7.2

Change your inteface as your JSON , you can do with JSON2TS JSON2TS更改为JSON,可以使用JSON2TS

 export interface RootObject {
        shortDescription: string;
        promotionName: string;
        highResolutionImage: string;
        lowResolutionImage: string;
        promotionOrChallengeCode: string;
}

and access it using 并使用

console.log(this.promotion.promotionOrChallengeCode);

The problem is your json interface needs to have same casing as your response from api. 问题是您的json接口需要与api响应具有相同的大小写。 Additionally you need to hand the HttpClient in angular the generic signature of your interface. 另外,您需要将HttpClient传递给接口的通用签名。

export interface IPromotion
{

    promotionOrChallengeCode: string;
    promotionName: string;
    shortDescription: string;
    highResolutionImage: string;
    lowResolutionImage: string;
}

promotion:IPromotion;

onSubmit() : void {

    this.httpClient.get<IPromotion>('http://localhost:8080/api/search/'+this.pcode )
    .subscribe((response:IPromotion) => 
          { 
            console.log(response);
            this.promotion = response; 
            console.log(this.promotion.promotionOrChallengeCode);
          });
    }

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

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