【问题标题】:Keep scrolling status in Angular portals在 Angular 门户中保持滚动状态
【发布时间】:2021-02-11 09:32:43
【问题描述】:

我遇到了角度门户的问题: 我有一个包含门户的页面和一些更改门户内容的触发器。都好。 当门户更改其内容时,它会将渲染(门户内部)组件的变量保存在内存中,例如增量;但遗憾的是不是滚动位置:每次门户更改时,滚动都会回到顶部。

有谁知道如何解决这个问题?

我在这里重现了问题StackBlitz

  • 点击“设置为 2”
  • 点击增量并向下滚动
  • 点击“设置为
  • 点击“设置为 2”
  • 查看您的最后一个增量,但滚动条位于顶部,而不是您离开的位置

【问题讨论】:

    标签: angular scroll portal


    【解决方案1】:

    在你的小部件中试试这个:

    import { CdkScrollable } from "@angular/cdk/overlay";
    import {
      AfterViewChecked,
      AfterViewInit,
      Component,
      ElementRef,
      Input,
      OnDestroy,
      ViewChild
    } from "@angular/core";
    
    @Component({
      selector: "tab2",
      template: `
        <portal>
          <div
            #scrollitem
            cdkScrollable
            *ngIf="target == 2"
            style="border-style: solid; height:200px; overflow: auto"
          >
            <p>Value: {{ tab2Value }}</p>
            <button (click)="increment()">INCREMENT</button>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
            <p>INSIDE PORTAL: TAB2</p>
          </div>
        </portal>
      `,
      styles: []
    })
    export class Tab2 implements AfterViewChecked, OnDestroy {
      @Input() target: number;
    
      @ViewChild("scrollitem", { read: CdkScrollable }) scrollitem: CdkScrollable;
    
      tab2Value = 0;
    
      scrolltop = 0;
    
      subs = null;
    
      constructor() {}
      ngOnDestroy(): void {
        if (this.subs) {
          this.subs.unsubscribe();
        }
      }
    
      ngAfterViewChecked(): void {
        if (this.scrollitem) {
          this.scrollitem.scrollTo({ top: this.scrolltop });
    
          this.subs = this.scrollitem.elementScrolled().subscribe(o => {
            this.scrolltop = this.scrollitem.measureScrollOffset("top");
          });
        }
      }
    
      increment() {
        this.tab2Value++;
      }
    }
    

    Stackblitz 在这里: https://stackblitz.com/edit/angular-ivy-2uaxdz?file=src%2Fapp%2Ftab2.component.ts

    【讨论】:

    • 像魅力一样工作,非常感谢。我认为应该以只有一个订阅的方式处理订阅:这里它在每个 ViewChecked 上添加一个新的订阅。不是吗?
    • 你是对的。由于 *ngIf ...目标,我无法在 ngOnInit 或 ngAfterViewInit 中获得对滚动项的引用。这必须稍微调整一下:)
    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多