【问题标题】:How to bind an HTTP Angular 4/5 request to html?如何将 HTTP Angular 4/5 请求绑定到 html?
【发布时间】:2018-08-23 19:31:15
【问题描述】:

我从一个有角度的 HTTPClient GET 请求返回了一些原始 JSON,我不确定现在如何将我的 JSON 对象的属性动态绑定到我的 html。

我通常会认为只是将返回的 JSON 对象存储到一个变量中,然后使用点表示法在我需要的地方引用它,但 Angular 似乎无法以这种方式工作,因为我无法将我的 http get 请求设置为变量在 ngOnInit 中并引用它。

当我的组件加载并成功登录到控制台时,我正在使用 ngOnInit 对其进行初始化,但是如何将其绑定到我的 html 中?

app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
  title = 'Contacts';
  constructor (private httpClient: HttpClient) {}

  ngOnInit(): void {
    this.httpClient.get('**URL PATH RETURNING JSON OBJECT**')
    .subscribe((data) => {
      console.log(data));
  }
}

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';


import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

HTML:

<div id= contacts-container>
  <header><h1> {{ title }} </h1></header>
  <div id= "favoritesContainer">
    <p>Favorite Contacts</p>
  </div>
  <ul>
    <li *ngFor="let contact of contacts">
      <div *ngIf= "!contact.isFavorite">
          <img src={{contact.smallImageURL}} />
          <h3><img src="../assets/Favorite Star (True)/Favorite — True.png">{{ contact.name }} </h3>
          <br>
          <p>{{ contact.companyName }}</p>
          <hr>
      </div>
    </li>
  </ul>
</div>

【问题讨论】:

  • 发布您收到的响应样本数据

标签: javascript json angular object angular-components


【解决方案1】:

您的app.component.ts 中似乎没有contacts 变量。

它应该是这样的:

export class AppComponent {
  title = 'Contacts';
  contacts: any[]; // Add variable
  constructor (private httpClient: HttpClient) {}

  ngOnInit(): void {
    this.httpClient.get('**URL PATH RETURNING JSON OBJECT**')
    .subscribe((data)=>{
         console.log(data);
         this.contacts = data; // Once you get the data, assign the data returned to contacts
    });
  }
}

【讨论】:

  • 您还应该将联系人初始化为空数组,以确保在出现大数据情况时避免任何未定义的错误
  • 非常感谢您的回复!运行此./src/app/app.component.ts Module parse failed: Unexpected token (18:28) You may need an appropriate loader to handle this file type. 时出现错误。当我在没有contacts = any[] 的情况下运行它时,它可以与添加到ngOnInitthis.contatcts 一起使用。为什么那行不通?
  • 因为我最终可能需要对 JSON 对象进行排序。
  • 我想我找到了。你的回答有一个轻微的错字。 contacts = any[]; 应该是 contacts: any[];
【解决方案2】:

试试这样:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
  title = 'Contacts';
  constructor (private httpClient: HttpClient) {}

  ngOnInit(): void {
    this.httpClient.get('**URL PATH RETURNING JSON OBJECT**')
    .subscribe((data)=>{
         this.contacts = data;//I have assigned data here inside subscription
         console.log(data);
    });
  }
}

并以与 HTML 相同的方式引用 this.contacts

【讨论】:

  • 这行得通。这是一个完全的疏忽,我觉得很傻。非常感谢!但是,我正在尝试使用contacts = any[]; 将其保存到我的constructor() 中的一个变量中,但它似乎不起作用。我收到了一个编译错误,我在此线程的另一个答案中回答了该错误。
  • 猜你得到了答案contacts:any[]
猜你喜欢
  • 2018-03-30
  • 2018-10-09
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
相关资源
最近更新 更多