0

I have a MatDialog (with MatDialogContent), which is bigger than the screen, so the vertical bar appears. After the unsuccessfull submit and all form's fields revalidation, I want to conditionally scroll to bottom of that dialog, to show MatError to the user. How to achieve that?

thinktanker
  • 129
  • 2
  • 12

1 Answers1

1

I implemented programatically scroll on div but not sure on MatDialogContent but you can give a try.

<div #scrollMe style="overflow: scroll; height: xyz;">
    // ...Scroling content
</div>

Then in component.ts

import {..., ElementRef, ViewChild, OnInit} from 'angular2/core'
@Component({
    ...
})
export class ChannelComponent implements OnInit{

    @ViewChild('scrollMe') private myScrollContainer: ElementRef;

    scrollToBottom(): void {
        try {
            this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
        } catch(err) {

        }                 
    }

    onError() {
        // Call scroll to bottom when you want to show error at bottom.
        this.scrollToBottom();
    }
}
  • Thanks it works! You just have to use MatDialogContent as the container. Also If you have dynamically added to DOM element (like my MatError with *ngIf clause), you must detect changed before scroll, so the dialog scroll moves below MatError too). – thinktanker Jun 12 '19 at 04:57