【问题标题】:Angular 6 ngFor list does not update automatically after new insertionAngular 6 ngFor 列表在新插入后不会自动更新
【发布时间】:2018-10-12 13:22:18
【问题描述】:

我正在构建一个 MEAN 应用程序,但我遇到了 *ngFor 列表在我创建新项目后没有更新的问题。如果我手动刷新,我知道该项目已成功创建,因为列表会更新,但它不会自动更新。

my.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { myThings } from '../things';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import { tokenNotExpired } from 'angular2-jwt';

export class AuthService {
constructor(private http:Http, private hTTp:HttpClient) { }
getThings():Observable<myThings[]>{
  const token = localStorage.getItem('id_token');
  let headers = new HttpHeaders();
  headers = headers.append('Authorization', token);
  headers = headers.append('Content-Type', 'application/json');
  return this.hTTp.get<myThings[]>('http://localhost:3000/things/get', { headers: headers });
}
}

things.ts

export interface myThings {
    _id:String,
    user:String,
    name:String,
    devicetype:String,
    state: Boolean,
    analog:Number,
    timestamp:String
}

things.components.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { ValidateService } from '../../services/validate.service';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs';
@Component({
  selector: 'app-things',
  templateUrl: './things.component.html',
  styleUrls: ['./things.component.css']
})
export class ThingsComponent implements OnInit {
  things = [];
  noThings: Boolean;

  constructor(private authService:AuthService, private validateService:ValidateService, private http:Http) {
  }

  ngOnInit() {
    this.authService.getThings().subscribe(things => {
      if (things) {
        this.things = things;
        this.noThings = false;
      }
      else {
        this.noThings = true;
      }
    })
  }
}

things.component.html

<div *ngIf="!noThings">
  <a href="" data-toggle="modal" data-target="#createThing">Create thing</a>
  <ul *ngFor="let thing of things">
    <li>Thing: {{thing.name}} Type: {{thing.devicetype}}</li>
  </ul>
</div>

数据添加成功后,列表没有更新的原因是什么?

请注意,我已阅读所有其他相关问题,但无法解决此问题。

【问题讨论】:

  • 您是否在订阅中收到了一个新的数组实例,还是只是添加了一个新条目?
  • 您是否在浏览器的网络面板中验证了GET请求成功。
  • @Shivam,我相信 getThings 只会触发一次 onInit。如果您想更新 things[],​​您需要再次调用它,或者在成功创建事物后将您的事物推送到事物(本地)。
  • @Farasi78 谢谢!我创建了一个函数并在 onInit 以及每次创建、删除或更新项目时调用它。

标签: angular typescript mean-stack


【解决方案1】:

正如@Farasi78 所提到的,getThings 只被调用了一次。因此,我创建了一个函数 handleThingList(),它在 onInit 以及每次在列表中插入新项目时都会调用

handleThingList()

this.authService.getThings().subscribe(things => {
      if (things) {
        this.things = things;
        this.noThings = false;
      }
      else {
        this.noThings = true;
      }
    })

我不知道这是否是正确的方法。但它有效!

感谢大家的热心回答!

【讨论】:

  • 希望如果数据是从 api 或 db 获取并加载的,这可以工作。目前我正在使用 localstorage,似乎它没有加载当前数据。刷新后或将我们的列表移回组件后,我最终会得到正确的列表。
猜你喜欢
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 2016-05-19
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多