rxjs - Angular 2 Observable dropping out of angular zone -
angular 2 application of state of application lives in @injectable services expose state via observable. @ point application dropping out of angular zone, causes change detection not work. in subscribe need call changedetectorref.detectchanges()
when occurs. how make sure application doesn't drop out of angular zone? or determine causing it?
here service:
@injectable() export class calendarfilterstore { private currentstate: calendarfilter; private state$ = new replaysubject<calendarfilter>(1); constructor() { this.currentstate = { startdate: new date(2016, 10, 14, 2) }; this.state$.next(this.currentstate); } state(): observable<calendarfilter> { console.log('calendar filter state, zone: ' + zone.current.name); return this.state$.asobservable(); }
here ngoninit function component using service:
ngoninit() { console.log('ng init zone: ' + zone.current.name); this.calendarfilterstore.state .filter(state => { console.log('got filter state, zone: ' + zone.current.name); return (state.areaids !== undefined && state.worksiteid !== undefined && state.startdate !== undefined && state.numberofshifts !== undefined); }) .switchmap(state => this.getcalendar(state)) .subscribe(res => { console.log('got calendar, dashboard-table, zone: ' + zone.current.name); this.loadcalendar(res); }, error => { console.log(error); });
here console output shows when drop out of angular zone. appears happen when calendar filter emits it's second value.
calendar filter state, zone: angular calendar-filter - store.ts:21 calendar filter state, zone: angular calendar-store.ts:22 calendar state, zone: angular dashboard-table.component.ts:60 ng init zone: angular calendar-filter - store.ts:21 calendar filter state, zone: angular dashboard-table.component.ts:63 got filter state, zone: angular dashboard-table.component.ts:63 got filter state, zone: <root> dashboard-table.component.ts:63 got filter state, zone: <root> calendar.service.ts:25 calendar, zone: <root> calendar.service.ts:31 got calendar, zone: <root> dashboard-table.component.ts:68 got calendar, dashboard-table, zone: <root> calendar-store.ts:27 segments returned, zone: <root>
i've narrowed down components several levels deep component tree. when check zone.current.name in constructors in root
zone , not angular
zone. events emit remain in root
zone, propagate parent->service->subscriber.
here component template works , runs in angular zone, child, sb-worksite-selector, runs in root zone, seen output.
<div class="dashboard-navigation" *ngif="optionsloaded"> <button type="button" class="btn btn-secondary" (click)="previousday()"><i class="fa fa-backward" aria-hidden="true"></i> previous day</button> <div class="inline-block"> <sb-worksite-selector [worksiteoptions]="worksiteoptions" (onworksitechanged)="worksitechanged($event)"> </sb-worksite-selector> </div>
sb-worksite-selector
@component({ selector: 'sb-worksite-selector', templateurl: 'worksite-selector.component.html', styleurls: ['worksite-selector.component.scss'] }) export class worksiteselectorcomponent implements oninit { @input() worksiteoptions: array<any>; @output() onworksitechanged = new eventemitter<number>(); constructor() { console.log('create worksite selector, zone: ' + zone.current.name); } ngoninit() { console.log('init worksite selector, zone: ' + zone.current.name); } }
logging worksite selector:
calendar filter state, zone: angular dashboard - table.component.ts:63 got filter state, zone: angular calendar.service.ts:25 calendar, zone: angular dashboard - navigation.component.ts:63 dashboard navigation got areas , worksites, zone: angular worksite - selector.component.ts:14 create worksite selector, zone: <root> worksite - selector.component.ts:18 init worksite selector, zone: <root>
Comments
Post a Comment