简体   繁体   English

如何在 POST 后刷新 angular2 中的 observable

[英]How to refresh an observable in angular2 after a POST

I have a table that shows information, upon clicking on a table row, user can update the values once he completes the request, the table is not updated with the new values and I must do an F5 to refresh the view.我有一个显示信息的表,单击表行后,用户可以在完成请求后更新值,该表未使用新值更新,我必须按 F5 刷新视图。

How do I force a new get request upon completing the request如何在完成请求后强制执行新的 get 请求

here is the relevant code这是相关的代码

MY SERVICE我的服务

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';

import {Observable} from 'rxjs/Rx';

import { BehaviorSubject } from 'rxjs/BehaviorSubject';

// Import RxJs required methods
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import { Device } from '../model/Device';

@Injectable()
export class DeviceService {

  devices$: BehaviorSubject<Device[]>;

  constructor(private http : Http) {
    this.initializeDevices();
  }

  initializeDevices() {
    var url = "./api/read/network/device";
    //var url = '../assets/data/devices.json';
    if (!this.devices$) {
      this.devices$ = <BehaviorSubject<Device[]>> new BehaviorSubject(new Array<Device>());
      this.http.get(url)
        .map((res:Response) => res.json())
        .subscribe(devices => {
          this.devices$.next(devices);
      });
    }
  }

  subscribeToDevices(): Observable<Device[]>  {
    return this.devices$.asObservable();
  }

}

my component我的组件

import { Component, OnInit, ViewChild } from '@angular/core';

import { DeviceService } from '../../services/device.service';
import { MacService } from '../../services/mac.service';
import { Device } from '../../model/Device';

@Component({
  selector: 'app-device-table',
  templateUrl: './device-table.component.html',
  styleUrls: ['./device-table.component.sass']
})
export class DeviceTableComponent implements OnInit {

  devices;
 
  //reference to the datatable needed to count filtered rows
  @ViewChild
  ('dt') dt;

  constructor(private deviceService : DeviceService, private macService : MacService) { }

  ngOnInit() {
    this.registerEvents();
    this.getDevices();
  }

  getDevices(){
    this.deviceService.subscribeToDevices().subscribe(
      devices => this.processDevices(devices),
      error => console.log(error)
    );
  }

  processDevices(devices){
    //just data manipulation
  }

  emitShowDialogEvent(device) {
    if(!device.macAddress) {
      return;
    }
      this.macService.broadcastShowDialogEvent(device.macAddress);
  }

  registerEvents(){

    this.macService.dataChangedEvent.subscribe(event =>{
      console.log('data changed event');
      this.getDevices();  //not provoking a new GET request to the API :(
    });
  }

}

I have another component, MACService, that upon sending a POST request emits an event that is captured as dataChangedEvent我有另一个组件 MACService,它在发送 POST 请求时会发出一个事件,该事件被捕获为 dataChangedEvent

Two ways you can use.您可以使用两种方法。

One is call GET request inside POST subscribe event.一种是在 POST 订阅事件中调用 GET 请求。

this.guardianService.editGuardian(this.guardData).subscribe(res=> {
  this.loadData();  // this is function who makes GET request
});

The other way is to use ReplaySubject.另一种方法是使用 ReplaySubject。

private subject: Subject<any> = new ReplaySubject<any>(1);

get $getSubject(): Observable<any> {
  return this.subject.asObservable();
}

loadData() {
    this._http.get(url, header)
        .map(x=>x.json())
        .subscribe(data=> {
          this.subject.next(data);
        });
}

postData(data) .....

This is Service.这是服务。 Here here goes component这里是组件

this.serviceHandle.$getSubject.subscribe(res => {
  //here goes any thing you want to do.
});
this.serviceHandle.loadData(); // this function will make reqeust


this._accountService.editAccountData(account).subscribe(res=> {
  this.serviceHandle.loadData();
});

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

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