You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BehaviourSubject未触发Angular模板重渲染问题求助

OpenLayers聚合标记点击后Angular模板不触发重渲染问题求助

经过数日尝试多种方案仍未解决,特向社区求助:在OpenLayers地图上点击/选择聚合标记后,Angular模板无法触发重渲染。已尝试手动变更检测、超时、NgZone、带switch case的普通属性、BehaviourSubject等方法,均无效。

简化后的模板示例

<ng-container *ngIf="subject$ | async as result">
    <ng-container *ngIf="result?.length === 0">
        <!-- 未点击标记时显示的内容 -->
    </ng-container>
    <ng-container *ngIf="result?.length === 1">
        <!-- 点击包含1个项的标记时显示的内容 -->
    </ng-container>
    <ng-container *ngIf="result?.length > 1">
        <!-- 点击包含多个项的聚合标记时显示的内容 -->
    </ng-container>
</ng-container>

监听自定义事件的代码

@HostListener('window:featureClick', ['$event'])
onMarkerClick(event: CustomEvent) {
    event.stopImmediatePropagation();
    if (this.eg?.length > 0) {
        this.eg = [];
    }
    const clickedFeature = event.detail;
    clickedFeature.forEach((feature) => {
        const matching = this.results.find((res: Object) => res.id === feature);
        if (matching) {
            this.eg = [...this.eg, matching]
        }
    });
    this.subject$.next(this.eg);
}

即使BehaviourSubject已发射预期的正确值,模板始终只显示初始值(length===0),原本以为async管道会自动处理此问题。

更新:OpenLayers触发自定义事件的代码

selectSingleClick.on('select', function (event) {
    const ids: number[] = [];
    const features = event?.selected[0]?.getProperties();
    features?.features?.forEach((feat) => {
        const prop = feat?.getProperties();
        ids.push(prop?.ids);
    });
    const featureClickEvent = new CustomEvent('featureClick', {
        detail: ids
    });

    window.dispatchEvent(featureClickEvent);
});

内容的提问来源于stack exchange,提问作者Vanonckelen Dieter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 14:53:24