I am trying to understand how Directives work. I am trying to use a Directive to scroll to an alert dialog when it pops up.
HTML
<div *ngIf="errorMsg" class="alertContainer" ScrollTo>
{{ errorMsg }}
</div>
ScrollToDirective.ts
import { AfterViewInit, Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[ScrollTo]'
})
export class ScrollToDirective implements AfterViewInit {
constructor(private elRef:ElementRef) { }
ngAfterViewInit() {
this.elRef.nativeElement.scrollIntoView();
}
}
I searched Stackoverflow and found this solution Angular2 scroll to element that has *ngIf
I tried to replicate it and had no luck. Can anyone shed some light as to why the page isn't scrolling?