簡體   English   中英

$timeout/setTimeout 回調 function 會在 UI 更新之前執行嗎?

[英]Will $timeout/setTimeout callback function executes before UI updates?

這個問題實際上是在我寫 AngularJS 1.6.10 時發生的,但我認為$timeout在某種程度上是一個setTimout ,所以我提到了兩者。

長話短說,我有兩個div可以根據使用ng-if的條件進行切換

<div ng-if="!vm.isDrillDowned" id="{{vm.segContainerId}}" style="height: 100%; width: 100%;"></div>
<div ng-if="vm.isDrillDowned" id="{{vm.drillDownContainerId}}" style="height: 100%; width: 100%;">

控制ng-if條件的代碼是:

public renderReport(filters){   
    if (//some condition) {
        reportId = this.drillDownId;
        containerId = this.drillDownContainerId;
        this.isDrillDowned = true;
    }

    if (//some condition) {
        reportId = this.segId;
        containerId = this.segContainerId;
        this.segmentFilter = null;
        this.isDrillDowned = false;
    }
    //Expect $timeout to refresh UI showing one div, then runs this.render()
    this.$timeout(() => this.render(reportId, containerId, filtersCopy));
}

this.render()

public render(reportId: string, containerId: string, filters?: any) {
    const container = document.querySelector(`#${containerId}`);
    this.$window.ThirdPartySDK.renderReportPart(container, /*other parameters */);
}

當我調試時,有時當代碼在 $timeout 內命中 function 時,我可以看到isDrilledDown已更改,但ng-if尚未刷新視圖,因此querySelector找不到與containerId相關的 DOM 元素。

根據 JS 的事件循環和 $timeout/setTimeout 的機制,我假設this.render只會在 UI 更新后運行,但有時並非如此。 ng-class在另一個地方也發生了同樣的問題,當在 $timeout 內運行時,元素 class 不會更新。

我只是想知道在某些極端情況下這種行為是否可能? 我所搜索的只是說 $timeout/setTimeout 將在 UI 更新后運行它的回調。

提前致謝!

如果不是這種情況,我深表歉意。 但是我是新來的,所以我沒有要評論的聲譽點。

您的 function 聲明是獨一無二的。 我不知道可以或應該從 function 的參數運行代碼。

也許這只是一個復制/粘貼錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM