[英]How can I activate and deactivate styles in component from typescript from another component in Angular 6?
I have in my app.component.ts
我在我的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');
}
});
In navbar.component.html
I have在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>
I would like to activate and deactivate styles automatically from navbar.component.scss
我想从navbar.component.scss
自动激活和停用样式
.nav-item .nav-link:not(.btn){
color: black;
border-color: black;
}
in app.component.ts
in this place在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');
}
Any ideas how can I do this?任何想法我该怎么做?
I think what you are looking for is the ngClass.我认为您正在寻找的是 ngClass。
In the html:在 html 中:
<inptut [ngClass]="'navbar-transparent', boolean" />
What this will do is add the style if the boolean is true and remove it when the boolean is false.这将做的是在布尔值为真时添加样式,并在布尔值为假时删除它。 If you are using a parent child structure you can use @Input or @Output to set the value of boolean.如果您使用父子结构,您可以使用@Input 或@Output 来设置布尔值。 Otherwise you can use a shared service.否则,您可以使用共享服务。
Sources: https://angular.io/api/common/NgClass https://angular.io/guide/component-interaction来源: https : //angular.io/api/common/NgClass https://angular.io/guide/component-interaction
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.