代码之家  ›  专栏  ›  技术社区  ›  tilly

角度:无法在元素中向下滚动到底部

  •  1
  • tilly  · 技术社区  · 7 年前

    我一直在推迟修复这个错误,我已经有一段时间了。我有下面的聊天窗口:

    enter image description here

    当我们从父组件接收带有消息的会话时,通过异步请求从服务器接收会话,我们希望滚动到window元素的底部。我们打电话给 this.scrollToBottom()

    This.scrollToBottom确实会被调用,但它不会滚动到元素的底部。有人知道为什么吗?

    export class ChatboxWindowComponent implements OnChanges, OnInit, AfterViewChecked {
    
      @Input('conversation') conversation;
      @ViewChild('window') window;
    
      constructor() { }
    
      ngOnChanges() {
        // If the date separators have already been added once, we avoid doing it a second time
        const existingDateObj = this.conversation.messages.findIndex((item, i) => item.dateObj);
    
        if (existingDateObj === -1) {
          this.conversation.messages.forEach( (item, index, array) => {
            if (index !== 0) {
              const date1 = new Date(array[index - 1].date);
              const date2 = new Date(item.date);
    
              if (date2.getDate() !== date1.getDate() || date2.getMonth() !== date1.getMonth()) {
                this.conversation.messages.splice(index, 0, {date: date2, dateObj: true});
                console.log(this.conversation.messages.length);
              }
            }
          });
        }
    
        this.scrollToBottom();
      }
    
      ngOnInit() {
      }
    
      ngAfterViewChecked() {
      }
    
      isItMyMsg(msg) {
        return msg.from._id === this.conversation.otherUser.userId;
      }
    
      scrollToBottom() {
        try {
          console.log('scrollToBottom called');
          this.window.nativeElement.top = this.window.nativeElement.scrollHeight;
        } catch (err) {}
      }
    }
    

    chat-window.component.html文件

    <div #window class="window">
      <ng-container *ngFor="let message of conversation.messages">
        <div class="date-container" *ngIf="!message.msg; else windowMsg">
          <p class="date">{{message.date | amDateFormat:'LL'}}</p>
        </div>
        <ng-template #windowMsg>
          <p
            class="window__message"
            [ngClass]="{
        'window__message--left': isItMyMsg(message),
        'window__message--right': !isItMyMsg(message)
        }"
          >
            {{message.msg}}
          </p>
        </ng-template>
      </ng-container>
    </div>
    
    1 回复  |  直到 7 年前
        1
  •  9
  •   ConnorsFan    7 年前

    scrollToBottom . 要在信息显示后滚动,请设置 template reference variable (例如。 #messageContainer )在消息容器上:

    <ng-container #messageContainer *ngFor="let message of conversation.messages">
      ...
    </ng-container>
    

    ViewChildren QueryList.changes 触发事件:

    @ViewChildren("messageContainer") messageContainers: QueryList<ElementRef>;
    
    ngAfterViewInit() {
      this.scrollToBottom(); // For messsages already present
      this.messageContainers.changes.subscribe((list: QueryList<ElementRef>) => {
        this.scrollToBottom(); // For messages added later
      });
    }
    
        2
  •  0
  •   Tharindu Lakshan    5 年前

    可以将以下代码添加到HTML元素中。

    #window [scrollTop]="window.scrollHeight" *ngIf="messages.length > 0"
    

    <div #window [scrollTop]="window.scrollHeight" *ngIf="messages.length > 0" class="window">
      <ng-container *ngFor="let message of conversation.messages">
        <div class="date-container" *ngIf="!message.msg; else windowMsg">
          <p class="date">{{message.date | amDateFormat:'LL'}}</p>
        </div>
        <ng-template #windowMsg>
          <p
            class="window__message"
            [ngClass]="{
        'window__message--left': isItMyMsg(message),
        'window__message--right': !isItMyMsg(message)
        }"
          >
            {{message.msg}}
          </p>
        </ng-template>
      </ng-container>
    </div>
    

    这是我的工作(目前,我正在使用Angular 11)