0
我想從組件A(模態窗口)讀取從一組值中選擇的值。一旦在組件A上按下ok按鈕,我希望將值傳遞給組件B(頁面組件)。我有這方面的服務,並遵循這裏指定的示例Delegation: EventEmitter or Observable in Angular2在2組件之間傳遞數據的可觀察/訂閱
我看到主題更新.next()。但是從我的用戶,我沒有看到,當我試圖從成分B訪問它的更新值
請幫我看看到底是什麼問題在這裏,
XService.ts
import { Injectable } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Observable } from 'rxjs';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
@Injectable()
export class XService {
constructor(private modalService: NgbModal) {}
// Observable Item source
public _adModalSelectedSource = new BehaviorSubject<string>('w');
// Observable Item stream
selectedRow$ = this._adModalSelectedSource.asObservable();
// service command
changeNav(string) {
this._adModalSelectedSource.next(string);
}
}
元器件A.ts
btnClick(btn) {
if (btn.id === 'ok') {
this.XService.changeNav('yes');
this.activeModal.close(this.selectedRows);
} else {
this.activeModal.dismiss();
}
}
ComponentB.ts
import { Component, ViewChild} from '@angular/core';
import { NgbDropdownConfig, NgbTabset } from '@ng-bootstrap/ng-bootstrap';
import { Subscription} from 'rxjs/Subscription';
import { XService} from '../../x/x.service';
import { ActivatedRoute, Params } from '@angular/router';
@Component ({
selector: 'compb',
templateUrl: './compb.html',
styleUrls: ['./compb.component.scss'],
providers: [xService, NgbTabset]
})
export class ComponentB {
xService: XService;
test: any;
subscription:Subscription;
route: any;
tabs: any;
subTabs: { all: 'all', problemMachines : 'problem_machines' };
constructor(X:XService, tabs: NgbTabset, route: ActivatedRoute) {
this.xModalService = X;
this.route = route;
this.tabs = tabs;
this.subTabs = { all: 'all', problemMachines : 'problem_machines' };
}
SelectHandler(data) {
if (data.item.id === 'XXX') {
this.XService.openModal().then(() => {
**console.log('test value'+ this.test);**
console.log('close');
//this._loadData();
});
}
}
ngOnInit() {
this.filterText = '';
this.subscription = this.xService.selectedRow$.subscribe(test => this.test = test);
}
ngOnDestroy() {
//on destroy hook
this.subscription.unsubscribe();
}
}
SelectHandler是模塊(組件A打開)用戶選擇一個值然後需要組件B(頁面)處理的值的按鈕的事件處理程序。
在組件B中,我將訂閱放在onINit()中,但this.test沒有更改的值。
任何投入,將不勝感激