簡體   English   中英

Angular不會從json服務更新模型

[英]Angular doesn't update model from json service

版本:節點:10.7.0 npm:6.1.0

架構:“我的頁面”是一個簡單的概念,它將呈現放置在資產文件夾中的新照片,並將信息添加到放置在資產文件夾中的json文件中。 我有一個讀取json文件的服務,然后將Observable發送到具有圖像位置和其他一些字段的組件。

問題當我發布站點時,所有瀏覽器都會渲染上次發布的內容,而不顯示更新的圖片或json文件。

當我在本地運行時,沒有問題,所有照片都會顯示。

有人可以幫忙說明一下,謝謝。

代碼如下所示:

JSON

[
  {
    "id": "",
    "name": "pic1",
    "location": "../../assets/photos/pic1.jpg",
    "date": "4/28/2018",
    "comments": "this is pic1"
  },
  {
    "id": "",
    "name": "pic2",
    "location": "../../assets/photos/pic2.jpg",
    "date": "4/28/2018",
    "comments": "this is pic2"
  }
]

服務

 getAllPhotos(){
   photoPath = '../../assets/photoInfo.json';
   return this.http.get(this.photoPath);
 }

組件

  data : any;

  constructor(private _data: PhotoAlbumService,private zone : NgZone,  public cd: ChangeDetectorRef) {
    this.data = new Array<IPhoto>();
  }
  getRecentPhotos(): void {
        this._data.getAllPhotos()
            .subscribe(recent => { 
                 this.zone.run(() => { // <== added
                     this.data = recent;
                 });
        });
    }

  ngOnInit() {
    let timer;
    this.getRecentPhotos();
    timer = timer(2000, 5000);
    timer.subscribe(() => this.getRecentPhotos());
  }

似乎GET請求已緩存。 您可以將Cache-Control選項設置為no-cache,以阻止HTTP請求被緩存。

檢查此線程以獲取更多詳細信息: 防止Angular 2中的IE11緩存GET調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM