【问题标题】:Why am I not able to assign boolean variable inside Observable?为什么我不能在 Observable 中分配布尔变量?
【发布时间】:2019-09-27 05:50:21
【问题描述】:

在从服务获取数据后,我试图隐藏一个 div 元素,您可以在下面找到 HTML 代码

<progressbar
                *ngIf="isLoadingBootStockData"
                [value]="100"
                type="default">
</progressbar>

最初我将变量 isLoadingBootStockData 分配给 false 并在我从服务中获取所有数据后分配回 true 所以 typeScript 代码如下所示:

return this.cseStockService
    .getBootStockMaterials()
      .pipe(
         map((materialStock) => {
         this.isLoadingBootStockData = false;
const MaterialList = this.cseStockService.createCseStockMaterialModels(
                                materialStock,
                                this.model.notification,
                                this.model.listMaterial,
                                CSE_STOCK.BOOT_STOCK
                            );
}).subscribe() 

所以在上面的代码第 5 行中,我将从 Service 获取数据并将变量分配给 this.isLoadingBootStockData = false; 以便 html div &lt;progressBar&gt;...&lt;/progressBar&gt; 得到隐藏

但是,这里的问题是,变量没有被赋值为 false。由于我在 observable 中使用,这不可能吗?

如果没有,我该怎么办?我想在加载数据后将该变量设为 false。

【问题讨论】:

    标签: angular html typescript observable angular7


    【解决方案1】:

    Observable 将在您订阅后执行。您必须在 subscribe 方法中添加以下行作为

        return this.cseStockService
            .getBootStockMaterials()
              .pipe(
                 map((materialStock) => {
    
        const MaterialList = this.cseStockService.createCseStockMaterialModels(
                                        materialStock,
                                        this.model.notification,
                                        this.model.listMaterial,
                                        CSE_STOCK.BOOT_STOCK
                                    );
        }).subscribe((res:any)=>{
           this.isLoadingBootStockData = false;
        }); 
    

    【讨论】:

    • 不.. 它不工作.. 我想我有绑定问题。它在 HTML 中根本没有绑定。我在 HTML 中添加了{{isLoadingBootStockData}},它返回的是 true 而不是 false。
    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2017-08-16
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多