简体   繁体   English

Angular - 刷新内容的问题

[英]Angular - issue with refreshing content

I have made a basic app for managing a database.我制作了一个用于管理数据库的基本应用程序。 The data is presented in a table and buttons for deleting each item.数据显示在表格和用于删除每个项目的按钮中。 For some reason, when deleting an item, the data does not refresh on the first click of the button but the second.出于某种原因,删除项目时,数据不会在第一次单击按钮时刷新,而是在第二次单击时刷新。 The second time the button is clicked, the console outputs a 404 error for the http request, because the item doesn't exist.第二次单击该按钮时,控制台为 http 请求输出 404 错误,因为该项目不存在。

I'm wondering why the data doesn't reload before then.我想知道为什么数据在那之前没有重新加载。 This is my component:这是我的组件:

import { Component, OnInit } from '@angular/core';
import { BackendService } from 'src/app/backend.service';
import { ICity } from '../interfaces/city';
import { ICountry } from '../interfaces/country';

@Component({
  selector: 'app-cities',
  templateUrl: './cities.component.html',
  styleUrls: ['./cities.component.css']
})
export class CitiesComponent implements OnInit {
  public countries: ICountry[] = [];
  public cities: ICity[] = [];

  constructor(private _backendService: BackendService) { }

  ngOnInit(): void {
    this.loadCities();
    this.loadCountries();
  }

  loadCities() {
    this._backendService.getCities().subscribe(data => {
      this.cities = JSON.parse(JSON.stringify(data)).cities;
    });
  }
  
  loadCountries() {
    this._backendService.getCountries().subscribe(data => {
      this.countries = JSON.parse(JSON.stringify(data)).countries;
    });
  }

  submit(city: ICity) {
    this.remove(city);
    this.ngOnInit();
  }

  remove(city: ICity): void {
    this.countries.forEach((cn) => {
      if (cn.majorCities.find(c => c.name == city.name)) {
        console.log(cn);
        this._backendService.removeCity(cn.name.toLowerCase(), city.name.toLowerCase()).subscribe(data => console.log(data));
      }
    });
  }
}

Template:模板:

<h1>Cities</h1>
<tbody>
    <tr>
        <td><h3>City</h3></td>
        <td><h3>Population</h3></td>
        <td><h3>Area in km²</h3></td>
        <td><h3>City Rank</h3></td>
    </tr>
    <tr *ngFor="let city of cities">
        <td>{{ city.name }}</td>
        <td>{{ city.population }}</td>
        <td>{{ city.area }}</td>
        <td>{{ city.rank }}</td>
        <button type="button" (click)="submit(city)">Delete</button>
    </tr>
</tbody>

You may have a race condition here.你可能在这里有一个竞争条件。 You're submitting a request for deletion, but performing a refresh before a response has been received and handled.您正在提交删除请求,但在收到和处理响应之前执行刷新。 When working with RxJS subscriptions, you can do the following...使用 RxJS 订阅时,您可以执行以下操作...

this._backendService.removeCity(cn.name.toLowerCase(), city.name.toLowerCase()).subscribe(
    // This is the callback for when the response was successful.
    // No error was caught during the request.
    (data) => {
        // You can attempt a refresh here because we know the request has
        // completed and the row was deleted.
        this.refresh();
    },
    // This is the callback for when the response was not successful.
    // The backend service threw some sort of error, or the code ran into an error
    // during execution. You can handle the error here (display some message).
    (error) => {
        // Do something in response to the error.
    }
    // This is the code to run when the Observable has communicated that it has
    // completed. The observable has said "I'm done sending messages, there will be
    // no more," so do whatever you need to in response to that.
    () => {
        // Do something...
    }
);

I also would not recommend using ngOnInit for your refresh operation as it is a reserved method used by Angular ;我也不建议使用ngOnInit进行刷新操作,因为它是Angular 使用的保留方法 you may find that, by calling ngOnInit , more is being done than you might think.您可能会发现,通过调用ngOnInit ,所做的事情比您想象的要多。

ngOnInit() {
    this.refresh();
}

public refresh(): void {
    // Your refresh logic here.
}

Need Ellobartion.需要Ellobartion。 According to ts provided,Try to Update this.cities post deletion Call the service agian instead og ngOnInit根据提供的ts,尝试更新this.cities post删除调用服务agian而不是og ngOnInit

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

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