私は角度のコンテキストの外にある私のservice.tsにfunction noificationHandler()
があるとします。 noificationHandler()
は第三者によって呼び出され、noificationHandler()
は基本的に配列を消費し、自分のサービスに加入しているコンポーネントに配列を送出します。Angular2 zone.run()とChangeDetectorRef.detectChanges()
service.ts
public mySubject: Subject<any> = new Subject();
public myObservable = this.mySubject.asObservable();
constructor() {
this.registry.subscribe("notification.msg",this.noificationHandler.bind(this));
}
noificationHandler(data) {
this.publishUpdate(data)
}
publishUpdate(data) {
this.mySubject.next(data);
}
component.ts
constructor(private service: myService) {
this.service.myObservable.subscribe(list => {
this.list = list;
});
}
^^^この時点でテンプレートが"notification.msg"
ので、新しいデータ
で更新されていません角度の範囲外です。角度は です。このイベント("notification.msg")
が呼び出されると、アンギエ検出は実行されません。
変更検出を呼び出す2つの方法があります。個別に変更
constructor(private service: myService, private ref: ChangeDetectorRef) {
this.service.myObservable.subscribe(list => {
this.list = list;
this.ref.detectChanges(); // <==== manually invoking change detection
});
}
両方のオプションが動作を検出するためのコンポーネントを要求することにより、角度のzone.run()
this.registry.subscribe("a2mevent.notification.msg", this.ngZone.run(() => this.noificationHandler.bind(this)));
2)の内側にnoificationHandler()
をラップすることにより
1)!
1)detectChangesは()のみ、独自のコンポーネントの変更を検出しますか、それはまた、子コンポーネントに変化検出を実行します -
A --> root component
B
C
D // my component is here (4 levels of nesting)
質問を次のように そして、私のコンポーネント構造がありますか?
2)zone.run()は、ルートからリーフまでのすべてのコンポーネントの変更検出をトリガーしますか?
zone.run()とdetectChanges()の中で私は好奇心が強いですのパフォーマンス?