EventEmitter 是动态创建的
public AddEventHandler(EventName: string, EventHandler: (value: any) => void):any {
let ei: EventItem;
let isFirst = false;
if (!this.EventsList.has(EventName)) {
let EventKey = window.CallNetMethod(0, "GetUniqueString");
let Event = new EventEmitter<any>(true);
ei = new EventItem(EventKey, Event);
this.EventEmittersList.set(EventKey, Event);
NetObject.EventCallers.set(EventKey, this.RaiseEvent.bind(this));
并称
public RaiseEvent(EventKey: string, value: any)
{
// Если есть подписчики, то вызываем их
if (this.EventEmittersList.has(EventKey))
{
this.EventEmittersList.get(EventKey).emit(value);
}
}
但问题是表单不响应接收到的数据的变化。
如果我从同一个对象使用 Timer
public SetTimer(func:(value: any)=>void)
{
let timer = Observable.timer(2000, 1000);
timer.subscribe((value: any) => { func(value) });
}
然后它甚至通过调用一个空委托来更新表单
this.AddEventHandlerResult = this.WOWE.AddEventHandler("TestEvent3", (value: any) => { this.EventsRes.push(new EventRes("TestEvent3", value)); });
this.WOWE.SetTimer((value: any) => {});
当然,我可以将其更改为 Observable.create 但问题是是否可以使用 EventEmitter 以及如何使用?
更改为可观察
class EventEmitter
{
public data: Observable<any>;
public dataObserver: Observer<any>;
constructor() {
this.data = Observable.create((observer: any) => this.dataObserver = <Observer<any>>observer);
alert("this.data"+this.data);
}
public subscribe(EventHandler: (value: any) => void)
{
return this.data.subscribe(EventHandler);
}
public emit(value: any)
{
this.dataObserver.next(value);
}
public Complete()
{
this.dataObserver.complete();
}
}
效果是一样的。是不是上下文不一样。告诉我谁知道。
需要使用 NgZone Made my EventEmitter based on Subject
在组件构造函数中,我获取 NgZone 并基于 Subject 创建一个基于它的发射器
我在创建 EventEmitter 时使用的
关于区域的好文章什么是区域?