In angular, I use scrollDispatcher(checking scrolling) change variable flag at html DOM, but the DOM(ngif) not work, there is my testing code:
// html
<div *ngIf="scrollState">
scrolling!
</div>
// TS
import { ScrollDispatcher } from '@angular/cdk/scrolling';
...
scrollState = false;
...
constructor( private scrollDispatcher: ScrollDispatcher){
let self = this;
this.scrollDispatcher.scrolled().subscribe( function () {
self.scrollState = true;
console.log('now scrolling!', self.scrollState );
//checking scrollState, it's true
});
}
When I scrolling, the DOM is not show, but I checking self.scrollState is really true, why? I can't understand, please help me, thank you.
Specific example
//html
<div class="sectionStickyTitle" *ngIf="sectionStickyTitleState">
<h2>Test new title</h2>
</div>
<h1 class="section-h1" #sectionh1>I'm check top target</h1>
//TS
import { ScrollDispatcher } from '@angular/cdk/scrolling';
...
@ViewChild('sectionh1') sectionh1: ElementRef;
sectionStickyTitleState = false;
sectionhOffsetTop: number;
...
constructor(private scrollDispatcher: ScrollDispatcher, ...){
}
ngOnInit(){
...
this.scrollDispatcher.scrolled().subscribe(() => this.setSectionStickyTitle());
}
...
setSectionStickyTitle() {
this.sectionhOffsetTop = this.sectionh1.nativeElement.getBoundingClientRect().top;
this.sectionStickyTitleState = (this.sectionhOffsetTop <= 21) ? true : false;
console.log('sectionStickyTitleState --> ', this.sectionStickyTitleState);
console.log('sectionhOffsetTop --> ', this.sectionhOffsetTop);
}
I re-edited the problem, the purpose is the same, the flag can not be recognized by html, when the height of #sectionh1 is less than 21, flag must be true, it is also true, but *ngIf="sectionStickyTitleState"(flag) always unable to respond.
This really makes me hard to understand because console.log always means flag is true.