简体   繁体   English

如何使用angular7在网页中显示值

[英]How to display values in webpage using angular7

I got the values from the API and I can show in Console log.我从 API 获得了值,并且可以在控制台日志中显示。 But I can't view on webpage.但我无法在网页上查看。 I don't know what I do wrong thing there.我不知道我在那里做错了什么。 Can anyone update me how to implement and show the values to html?任何人都可以更新我如何实现和显示 html 的值吗? home.component.ts home.component.ts

import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
 users: Object;

constructor(private data: DataService) { }

ngOnInit() {
this.data.getUsers().subscribe(data => {
    this.users = data
    console.log(this.users);
  }
);
}
 firstclick(){
  console.log('clicked button')
}

} }

home.component.html主页.component.html

  <li *ngFor="let user of users.data">
    <p>
      {{ user.firstname }} 
    </p>
  </li>

data.service.ts数据服务.ts

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

@Injectable({
  providedIn: 'root'
})
export class DataService {

constructor(private http: HttpClient) { }

getUsers(){
    return this.http.get('http://localhost:3000/api/crs')
 }
}

console.log output console.log 输出

ERROR CONTEXT Object { view: {…}, nodeIndex: 3, nodeDef: {…}, elDef: {…}, elView: {…} } HomeComponent.html:4 (3) […] ​ 0: Object { firstname: "karthik", lastname: "selvam", id: 1 } ​ 1: Object { firstname: "ranjith", lastname: "kumar", id: 2 } ​ 2: Object { firstname: "sathish", lastname: "kumar", id: 3 } ​ length: 3 ERROR CONTEXT Object { view: {…}, nodeIndex: 3, nodeDef: {…}, elDef: {…}, elView: {…} } HomeComponent.html:4 (3) […] 0: Object { firstname: "karthik", lastname: "selvam", id: 1 } 1: Object { firstname: "ranjith", lastname: "kumar", id: 2 } 2: Object { firstname: "sathish", lastname: "kumar ", id: 3 } 长度: 3

Your API response data do not include data filed wrapper so remove data from html template home.component.html您的 API 响应数据不包括数据归档包装器,因此从 html 模板 home.component.html 中删除数据

 <li *ngFor="let user of users">
    <p>
      {{ user.firstname }} 
    </p>
  </li>

in home.component.ts you can change type of users property在 home.component.ts 中,您可以更改用户属性的类型

export class HomeComponent implements OnInit {
 users: any[];

constructor(private data: DataService) { }

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

相关问题 如何将 BigBlueButton 与 Angular7 应用程序集成? - How to integrate BigBlueButton with Angular7 application? 如何在使用 angular7 和 springboot 构建的 Web 应用程序中使浏览器缓存无效 - How to invalidate browser cache in a web application built with angular7 and springboot 如何将网页另存为网页存档,然后使用Android的WebView将网页存档显示为网页? - How to save webpage as web archive and then display the web archive as a webpage using Android's WebView? 自定义等待硒用于angular7应用 - Custom wait for selenium for angular7 application 使用HTML / JavaScript在网页中显示applet - Display an applet in a webpage using HTML/JavaScript 如何使用thymeleaf + spark-java web框架在网页中显示消息? - How to display message in webpage using thymeleaf + spark-java web framework? 如何在JSF中的网页上显示系统日期? - How to display system date on the webpage in JSF? 如何使用angular js动态更改网页上的状态消息以获得各种结果 - how to dynamically change the status message on the webpage for various results using angular js 如何使用Selenium Java将数组中的值分配给网页中的文本框 - How to assign values from an array to Text boxes in Webpage using selenium java 如何使用 Angular 4 在 Spring Boot 中显示存储的图像? - How to display stored images in Spring Boot using Angular 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM