Angular不会从json服务更新模型

时间:2018-07-29 15:39:59

标签: angular

版本: 节点: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());
  }

1 个答案:

答案 0 :(得分:0)

似乎GET请求已缓存。您可以将Cache-Control选项设置为no-cache,以阻止HTTP请求被缓存。

检查此线程以获取更多详细信息:Prevent IE11 caching GET call in Angular 2