繁体   English   中英

DataGrid 不包含在 Angular 中订阅 scope 的“this”上下文中

[英]DataGrid is not included inside of “this” context in subscribe scope in Angular

在我的 Angular 项目中,一个页面包含 2 个名为“head”和“product”的组件。

在“head”中,我可以更改页面的货币。我将货币信息保存在localStorage中。当货币更改时,我通过“next”推送一个随机数,如下所示:

this.service.refreshNumberOfAllPriceByCurrency.next(Math.random()); //"refreshNumberOfAllPriceByCurrency" is defined as ReplaySubject<any>(1) in service component.

此行在“产品”组件中触发订阅,如下所示:

constructor(.........)
{
   this.service.refreshNumberOfAllPriceByCurrency.subscribe(number => {
        this.gridProduct.instance.refresh();
        this.gridPrice.instance.refresh();
   });
}

在 subscribe scope 内部调试时,“this”上下文不包含 gridProduct、gridPrice 和 linensOfferId。 所以它无法获取“未定义”的实例......

如何在订阅 scope 中获取这些网格的实例?

提前致谢…

我解决了这个问题。 “this.service.refreshNumberOfAllPriceByCurrency.subscribe()”事件在构造函数中。 我修改了代码如下:

constructor()
{
   var this2=this;

   this.service.refreshNumberOfAllPriceByCurrency.subscribe(number => {
       if (this2.linensOfferId > 0) {
           this2.gridProduct.instance.refresh();
           this2.gridPrice.instance.refresh();
       }
   });
}

在 subscribe() 之前,我定义了 this2,它等于“this”。 通过这种方式,我可以完全从 subscribe() 范围内到达这个上下文......

谢谢…

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM