我使用的是Ionic 2,它位于Angular 2之上。我希望将一个课程添加到我的ion-header
元素中。这是为了让我的标题最初有一个透明的背景,但在滚动上有一个坚实的背景。如何在我的视图中实现动态类?
我在我的Typescript文件中设置一个变量,并在用户滚动时更新该变量。
我需要根据我的Typescript变量中的变量设置类。我在我的应用程序的其他地方以[ngClass]="{showBack: navBack}"
完成了此操作,但这似乎不适用于此页面。
打字稿:
import { Component, ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';
@Component({
selector: 'page-class',
templateUrl: 'class.html'
})
export class ClassPage {
@ViewChild(Content)
content: Content;
navBack: boolean = false;
constructor(...){}
ngAfterViewInit() {
this.content.ionScroll.subscribe((event) => {
if(event.scrollTop == 0) {
this.navBack = false;
} else {
this.navBack = true;
}
});
}
}
HTML:
<ion-header [ngClass]="{showBack: navBack}">
<ion-navbar>
<ion-title>Test</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<p style="height: 300px">Long page of content here.</p>
<p style="height: 300px">Long page of content here.</p>
<p style="height: 300px">Long page of content here.</p>
</ion-content>
我希望看到我的ion-header
出现在滚动一个showBack
CSS类,但它永远不会出现,不管event.scrollTop
值。
我已经在ngAfterViewInit
函数上用console.log
进行了一些调试,我可以确认变量是否正确更改,但类不更新。
只需添加引号即可尝试?我在试图帮助你的文档中发现了什么:https://angular.io/guide/template-syntax#binding-targets –