我的 app.component.ts
this.renderer.listenGlobal('window', 'scroll', (event) => {
const number = window.scrollY;
if ((number > 150 || window.pageYOffset > 150) && this.location.prepareExternalUrl(this.location.path()) == '/') {
// I want to activate here style .nav-item .nav-link:not(.btn) in navbar.component.html
navbar.classList.remove('navbar-transparent');
} else {
// I want to deactivate here style .nav-item .nav-link:not(.btn) in navbar.component.html
navbar.classList.add('navbar-transparent');
}
});
在 navbar.component.html
我有
<li class="nav-item" *ngIf="!isDocumentation() && !isMain() && !isCloud() && !isView()">
<a class="nav-link" rel="tooltip" title="Follow us on Twitter" data-placement="bottom"
href="https://example-link.com" target="_blank">
<i class="fa fa-twitter"></i>
<p class="d-lg-none">Twitter</p>
</a>
</li>
我想从 navbar.component.scss
自动激活和停用样式
.nav-item .nav-link:not(.btn){
color: black;
border-color: black;
}
在app.component.ts
这个地方
// I want to activate here style .nav-item .nav-link:not(.btn) in navbar.component.html
navbar.classList.remove('navbar-transparent');
} else {
// I want to deactivate here style .nav-item .nav-link:not(.btn) in navbar.component.html
navbar.classList.add('navbar-transparent');
}
我该怎么做?
最佳答案
我认为您正在寻找的是 ngClass。
在 html 中:
<inptut [ngClass]="'navbar-transparent', boolean" />
这样做的目的是在 bool 值为真时添加样式,在 bool 值为假时移除样式。如果您使用的是父子结构,则可以使用@Input 或@Output 来设置 bool 值。否则,您可以使用共享服务。
来源:https://angular.io/api/common/NgClass https://angular.io/guide/component-interaction
关于html - 如何从 Angular 6 中的另一个组件的 typescript 激活和停用组件中的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53314827/