Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngx-perfect-scrollbar: How to reference to the directive from typescript?

I'm using ngx-perfect-scrollbar with my Angular 5 project. I want to scroll the div to top when the route changes.

dashboard.html

<div class="main-panel" [perfectScrollbar] #perfectscroll>
    <router-outlet></router-outlet>
</div>

dashboard.ts

@Component({
    selector: 'app-dashboard',
    templateUrl: './dashboard.component.html',
})
export class DashboardComponent implements OnInit {
    @ViewChild('perfectscroll') perfectscroll: PerfectScrollbarDirective;

    ngOnInit() {
        this.router.events.subscribe((evt) => {
            if (!(evt instanceof NavigationEnd)) {
                return;
            }
            this.perfectscroll.scrollToTop()
        });
    }
}

But I get this error :

TypeError: _this.perfectscroll.scrollToTop is not a function

like image 414
blue Avatar asked Mar 06 '23 16:03

blue


2 Answers

Look at my working example.

In the trmplate:

<div class="perfectScroll" [perfectScrollbar] #psLeft="ngxPerfectScrollbar">...</div>
...
<div class="perfectScroll" [perfectScrollbar] #psRight="ngxPerfectScrollbar">...</div>

...

In the component:

@ViewChild('psLeft') psLeft: PerfectScrollbarDirective;
@ViewChild('psRight') psRight: PerfectScrollbarDirective;
...
if (this.psRight) {
    this.psRight.scrollToTop();
}
like image 127
Anton Pegov Avatar answered Mar 20 '23 17:03

Anton Pegov


just use this: @ViewChild(PerfectScrollbarDirective) perfectscroll: PerfectScrollbarDirective;

you will have instance and then you can call functions like scrollToTop()

like image 41
D013 Avatar answered Mar 20 '23 16:03

D013