简体   繁体   English

Angular4:从Json加载数据

[英]Angular4: Load data from Json

I'm a newbie in Angular. 我是Angular的新手。 What i'm doing is loading data from json file on click, which so far i have done correctly. 我正在做的是在点击时从json文件加载数据,到目前为止我已经正确完成了。

But the point where im stuck is to load the first json object before click, ie to load the first object when the page loads 但是我遇到的问题是在点击之前加载第一个json对象,即在页面加载时加载第一个对象

Typescript 打字稿

export class RequestComponent{
  people: Object[];
  constructor(http:Http){
    http.get('data.json').subscribe(res => {
      this.people = res.json();
    });
  }

  public currentStatus;
  public setThis = (people) => this.currentStatus = people;
}

Html HTML

  <ul class="col-md-4">
    <li *ngFor="let person of people" (click)="setThis(person)">
      <span><i class="fa fa-cog" aria-hidden="true"></i></span>
      <p>{{ person.name }}</p>
    </li>
  </ul>
   <div *ngIf="currentStatus" class="col-md-8 right-section">
      <h2>{{ currentStatus.name }}</h2>
      <img [src]="currentStatus.img" class="img-responsive"/>
   </div>

Try loading json in ngOnInit() function. 尝试在ngOnInit()函数中加载json。

import { Component, OnInit } from '@angular/core';
// other imports

export class RequestComponent implements ngOnInit{
  people: Object[];

  constructor(private http:Http){}

  ngOnInit(){
      this.http.get('data.json').subscribe(res => {
      this.people = res.json();
    });
  }
}

And adding ngIf for displaying your records, so it doesn't try to display it before people are loaded. 并添加ngIf用于显示您的记录,因此它不会尝试在加载people之前显示它。

<ul *ngIf="people" class="col-md-4">
    <li *ngFor="let person of people" (click)="setThis(person)">
        <span><i class="fa fa-cog" aria-hidden="true"></i></span>
        <p>{{ person.name }}</p>
    </li>
</ul>
<div *ngIf="currentStatus" class="col-md-8 right-section">
    <h2>{{ currentStatus.name }}</h2>
    <img [src]="currentStatus.img" class="img-responsive" />
</div>

Your page will usually load faster than it can get a response form your API, as such you should be setting people inside of the subscribe, after you get the response, so you can be certain it has received it. 您的页面加载速度通常比从API获得响应的速度快,因此您应该在获得响应后设置订阅者内部的人员,这样您就可以确定它已经收到了响应。

constructor(private http:Http){
    this.http.get('data.json').subscribe(res => {
        this.people = res.json();
        if(this.people && this.people.length>0)
            this.setThis(this.people[0]);
    });
}

Depending on your implementation, it may be more appropriate to do the if to ensure that the people array isn't empty inside of setThis. 根据您的实现,执行if可能更合适,以确保在setThis中people数组不为空。


Alternatively, you could change your html to be like so 或者,你可以改变你的html

<ul class="col-md-4">
    <li *ngFor="let person of people" (click)="setThis(person)">
        <span><i class="fa fa-cog" aria-hidden="true"></i></span>
        <p>{{ person.name }}</p>
    </li>
</ul>
<div *ngIf="currentStatus>-1" class="col-md-8 right-section">
    <h2>{{ people[currentStatus]?.name }}</h2>
    <img [src]="people[currentStatus]?.img" class="img-responsive"/>
</div>

Change currentStatus to a number and default it to 0. This would allow you to circumvent the problem of assigning currentStatus to the first person, as soon as there was a person the html would update. 将currentStatus更改为一个数字,并将其默认为0.这将允许您在html更新时立即避免将currentStatus分配给第一个人的问题。 If you ever needed to disable the display of the currentStatus part, just set it to -1. 如果您需要禁用currentStatus部分的显示,只需将其设置为-1即可。

The ? 的? before the .name and .img tells the html to verify the object exists before trying to access the name or img, if it doesn't exist it doesn't show that part. 在.name和.img之前告诉html在尝试访问名称或img之前验证对象是否存在,如果它不存在则不显示该部分。

Try this: 尝试这个:

constructor(private http:Http){
    this.http.get('data.json').subscribe(res => {
        this.people = res.json();
    });
}

ngAfterViewInit(){        
    this.setThis(this.people[0]);
}

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

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