Angular 2:是什么让服务成为“外部"的?角区? [英] Angular 2 : what make a service to be "Outside" angular zone?

查看:15
本文介绍了Angular 2:是什么让服务成为“外部"的?角区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 StackOverflow 上遇到与许多人相同的问题后,我无法理解什么是外部角度区域"服务?

After having same issues as many people on StackOverflow i didn't manage to understand what is an "Outside angular zone" Service ?

我已经检查了有关该主题的所有现有问题,这正是我需要问这个问题的原因:

I've checks all existing questions around this subject, and it's exactly why i needer to ask this one :

具有默认 ChangeDetectionStrategy 的任何组件中的代码示例:(考虑到视图中引用的 this.value)

Code Example in any component with default ChangeDetectionStrategy : (considering this.value referenced in view)

this.myService.method().subscribe(e => {
    this.value = e;
  });

给定的服务不是外部角度区域",视图刷新,另一方面,如果它是外部角度区域",视图没有刷新,除非我们调用 ChangeDetectorRef.detectChanges().

Is the given service is not "Outside angular zone", the view is refreshed, on the other hand, if it's "Outside angular zone", the view is not refreshed, unless we call ChangeDetectorRef.detectChanges().

所以问题是:知道服务是在角区"内部还是外部的条件是什么?

So the question is : what the condition to know if a service is Inside or Outside "Angular Zone" ?

推荐答案

你想要的代码是NgZone.isInAngularZone().这会告诉你它是否在那里执行.

The code you want is NgZone.isInAngularZone(). This will tell you whether or not it's executing there.

资料来源:在写这篇文章之前,我花了好几个小时阅读 Angular 文档.

Source: hours of banging my head against the wall reading Angular docs before writing this.

此外,您可以将 NgZone 注入您的服务并尝试使用 this.ngZone.run(() => yourSubscriberCallback()) 这应该会有所帮助,尽管我尝试这个的结果很复杂.

Additionally, you can inject NgZone into your service and try using this.ngZone.run(() => yourSubscriberCallback()) which should help, though I'm having very mixed results attempting this.

好的,我设法让我的东西正常工作,让我们看看它是否对你有帮助.

Okay, I managed to get my stuff working, let's see if it helps you.

在我的例子中,我使用了一个第三方库,其中包含一个更改监听器.我正在使用 RXJS BehaviorSubject 通过服务将这些更改传播到各种组件,但这些更改没有被拾取.

In my case I was using a third party library that included a listener for changes. I was using an RXJS BehaviorSubject to propagate these changes to various components via a service, but the changes weren't being picked up.

原来这是因为我在监听器中使用的方法是在 AngularZone 之外执行的.

It turns out that this was because the method I used in the listener was executing outside of the AngularZone.

一开始我是这样做的:

export class Service {

  public BehaviorSubject<Thing> thingSubject = new BehaviorSubject<Thing>(new Thing());

  constructor(private ngZone:NgZone) {
    thirdPartyLibrary.listen(ngZone.run(() => myCallback.bind(_this)));
  }

  ...

}

myCallback 正在做:

myCallback(thing) {
  this.thingSubject.next(thing);
}

事实证明,这似乎没有在 Angular 区域内正确执行.我把我的代码改成了这个,它起作用了:

Turns out this didn't seem to execute within the Angular Zone correctly. I changed my code to this though and it worked:

export class Service {

  public BehaviorSubject<Thing> thingSubject = new BehaviorSubject<Thing>(new Thing());

  constructor(private ngZone:NgZone) {
    thirdPartyLibrary.listen(myCallback.bind(_this));
  }

  myCallback(thing) {
    this.ngZone.run(() => this.thingSubject.next(thing));
  }

}

之后,我的所有订阅者都在 Angular Zone 内收到了消息并触发了预期的更新.

After doing that all my subscribers received the message within the Angular Zone and triggered the expected updates.

这篇关于Angular 2:是什么让服务成为“外部"的?角区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆