【问题标题】:DataGrid is not included inside of "this" context in subscribe scope in AngularDataGrid 不包含在 Angular 订阅范围内的“this”上下文中
【发布时间】:2020-05-30 02:00:17
【问题描述】:

在我的 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();
   });
}

在订阅范围内调试时,“this”上下文不包含 gridProduct、gridPrice 和 linensOfferId。所以无法获取“undefined”的实例……

如何在订阅范围内获取这些网格的实例?

提前致谢……

【问题讨论】:

  • 您是否尝试过在 ngOnInit 或 ngAfterViewInit 中订阅?
  • 你能在 stackblitz 上重现这个吗?
  • 嗨 Wctiger.. 我试过 ngAfterViewInit。没有任何改变。
  • 请说明您如何实例化 gridProduct 和 gridPrice。如果您收到它们未定义的错误,那么它们并没有在您希望它们被实例化时被实例化。
  • 我正在像这样实例化网格:@ViewChild("gridProduct", { static: false }) gridProduct: any; @ViewChild("gridPrice", { static: false }) gridPrice: any;

标签: angular observable instance subscribe


【解决方案1】:

我解决了这个问题。 “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() 范围内到达这个上下文......

谢谢……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多