How to create an observable in Angular 2

前端 未结 3 702
野性不改
野性不改 2020-12-28 14:48

I am trying to evaluate Angular 2 and I am having problems with observables.

I am trying to create a simple service that initially returns a hard coded array but wil

3条回答
  •  情深已故
    2020-12-28 15:10

    For this you can user BehaviorSubject

    export class FleetListService {
        public data: any = new BehaviorSubject('your hard coded data goes here');
        getdata() {
            //do your stuff
            this.FleetListService.next('new data got from server');
        }
    }
    

    One of the variants of Subjects is the BehaviorSubject, which has a notion of "the current value". It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "current value" from the BehaviorSubject. Behaviour subject always returns last data. For more info

提交回复
热议问题