我为表头( tad标签)创建了自己的指令。这使得表格的标题是“粘性的”(具有固定位置并且在滚动表格期间可见)。它看起来像这样:
粘性指令.ts
const NAVBAR_HEIGHT = 55;
@Directive({
selector: '[sticky]',
})
export class StickyDirective implements AfterViewInit {
@Input()
@HostBinding('style.width.px')
stickyWidth: string;
topPosition: number;
constructor(private _element: ElementRef, private _window: WindowRef) { }
ngAfterViewInit() {
let boundingClientRect = this._element.nativeElement.getBoundingClientRect();
this.topPosition = boundingClientRect.top - NAVBAR_HEIGHT;
}
@HostListener('window:scroll', ['$event'])
handleScrollEvent(e) {
if (this._window.nativeWindow.pageYOffset > (this.topPosition) ) {
this._element.nativeElement.classList.add('stick');
} else {
this._element.nativeElement.classList.remove('stick');
}
}
}
并以这种方式调用:
<div class="card-block" >
<table #table class="table table-bordered">
<thead sticky [stickyWidth]="table.offsetWidth">
它工作正常,但在控制台中我收到错误:
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '1632'. Current value: '1615'.
at viewDebugError (core.es5.js:8633)
at expressionChangedAfterItHasBeenCheckedError (core.es5.js:8611)
at checkBindingNoChanges (core.es5.js:8775)
at checkNoChangesNodeInline (core.es5.js:12329)
at checkNoChangesNode (core.es5.js:12303)
at debugCheckNoChangesNode (core.es5.js:12922)
at debugCheckDirectivesFn (core.es5.js:12824)
at Object.View_PageListComponent_0.co [as updateDirectives] (PageListComponent.html:23)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12806)
at checkNoChangesView (core.es5.js:12123)
at callViewAction (core.es5.js:12487)
谁能解释我做错了什么?如何保护我的应用免受此异常的影响,或者创建类似指令的正确方法是什么?提前致谢