【问题标题】:Item is deleted, but when you leave the page and come back the item is placed again even if it is not in the postman项目被删除,但是当您离开页面并返回时,即使它不在邮递员中,也会再次放置该项目
【发布时间】:2018-12-30 23:06:24
【问题描述】:

我正在开发一个有角度的 php 应用程序,并且我在 localhost 上运行它。在我的前端,我有一个检索我的信息并显示的功能。列表中的每一行都有一个名为删除的按钮,以便能够删除此项目,当我单击删除时,我会获取项目的 ID 并向我的服务器发送删除请求。我与邮递员一起遵循这一点,并且可以想象它确实删除了所有文件,但是在我的角度中,该项目仍然存在,即使我在尝试使用 get 方法检索列表时将其从列表中删除删除的项目回来了,但邮递员不回来。

Category.service.ts

  get(page, descricao) {
    page = 'all';
    return this.http.get(Auth.url + this.nameClass + `?page=${page}&desc=${descricao}`, { headers: { 'token': Auth.token } });
  }

  delete(id) {
    console.log(Auth.url + this.nameClass + "/" + id);
    this.http
      .delete(Auth.url + this.nameClass + "/" + id, { headers: { 'token': Auth.token } })
      .subscribe(dados => alert('Ok'), (error: any) => alert('erro'));
  }

当我使用函数删除警报('ok')时,因为我的服务器返回true,因为它已完美删除

CategoryList.Component.ts

items: any;
ngOnInit() {
    this.getList();
}

 excluir(desc, id) {

    if (confirm("Do you want to delete the category " + desc + " ?")) {
      let index = this.getIndex(id);
      if (index == -1)
        alert('Error');
      else {
        let result = this.categoriaService.delete(id);
        this.items.splice(index, 1);
      }
    }
  }

  getIndex(id) {
    for (let i = 0; i < this.items.length; i++)
      if (this.items[i].id == id)
        return i;
    return -1;
  }

  getList(page: number = 1) {

     this.categoriaService.get(page, this.pesquisarValue)
.subscribe(dados => this.exibeLista(dados), (error: any) => console.log(error));
      }



  exibeLista(dados) {

    this.items = null;

    if (dados.result.count == 0) // Quantidade de itens retornados da consulta
      this.pages = null;
    else {
      if (dados.result.paginas == 0) // Quantidade de paginas
        this.pages = null;
      else {
        this.totalPages = dados.result.paginas;
        this.loadPages();
      }
      this.items = dados.result.item;
    }

我使用 this.items.splice(index, 1) 函数;并从我的项目数组中删除已删除的项目,到目前为止一切顺利。当我离开此页面并返回时,该项目再次出现,即使它不在邮递员中。 我在 allow-control-allow-origin 的 chrome 中使用扩展名会导致任何问题吗?这是我在某个缓存中的列表吗?如何删除已删除的数据

【问题讨论】:

  • 请不要同时使用angularangularjs 来标记问题,除非你真的在做一个混合框架项目; 这些不是同一个框架
  • 您好,我是 Angular 新手,我也遇到了同样的问题。在服务器上,该项目被删除,但是当调用该函数以将其取回时。但是,如果您再次尝试删除它,它会像已被删除一样返回

标签: angular


【解决方案1】:

除了服务之外,您的代码看起来不错。请更改您的服务代码,如下所示

get(page, descricao) {
    page = 'all';
    return this.http.get(Auth.url + this.nameClass + `?page=${page}&desc=${descricao}`, { headers: { 'token': Auth.token } })
.map(response => return response ) // or try  `response.json()` instead of `response` 
.catch(error => {
     return Observable.throw(error);
   });;
  }

【讨论】:

  • 您好,我正在尝试使用您的方法并显示以下错误:“Observable 类型中不存在地图属性”,我的导入 import { Observable } from '../../ ../node_modules/rxjs';从 'rxjs/operators' 导入 { map };
  • @EmiryMirella 只需导入 import 'rxjs/Rx'; stackoverflow.com/questions/37208801/…
  • 我的角度是版本 6。我做到了,但仍然出现错误“node_modules / rxjs / Rx.d.ts (1.15) 中的错误:错误 TS2307: 找不到模块 'rxjs-compat'仍然存在。src / app / model / category.service.ts (35,6): error TS2339: Property 'map' does not exist on type 'Observable '. "
  • 那么你应该修复错误。因为每个服务调用我们都需要使用 map() 来表示 observable .,
  • 我可以在管道内使用地图吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 2018-09-21
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多