Angular-Material Sidenav cdkScrollable

后端 未结 2 1114
面向向阳花
面向向阳花 2020-12-30 05:23

The Angular Material CDK provides a Directive CdkScrollable, which allows you to listen to ScrollEvents of a specific container.

2条回答
  •  情话喂你
    2020-12-30 05:54

    I opened an Issue on @angular/material some time ago and they now expose the CdkScrollable-Instance.
    To use it, you need to access the MatSidenavContainer using @ViewChild(MatSidenavContainer. This instance has a public member scrollable, which is the CdkScrollable instance.
    An example can be found here

    Edit: As the example is not very complete and a few people are having difficulties implementing it, I'll write my own example here:

    HTML:

    
        
            Sidenav Content
        
        
    Main Content

    TypeScript:

    import { Component, AfterViewInit, ViewChild } from '@angular/core';
    import { MatSidenavContainer } from '@angular/material';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent implements AfterViewInit  {
      @ViewChild(MatSidenavContainer) sidenavContainer: MatSidenavContainer;
    
        constructor() {
        }
    
        ngAfterViewInit() {
          console.log(this.sidenavContainer.scrollable);
        }
    }
    

    Important:

    1. Don't use . This tag is generated automatically and it has the cdkScrollable directive attached to it. If you use in your own template, the scrollable will be undefined.
    2. Use AfterViewInit instead of OnInit. As much as I know, @ViewChild is resolved in AfterViewInit, OnInit is probably too early.

提交回复
热议问题