简体   繁体   English

如何从 Angular 6 中的另一个组件的打字稿激活和停用组件中的样式?

[英]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 havenavbar.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 placeapp.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.

相关问题 如何从一个组件路由到 Angular 中另一个组件的一部分? - How can I route from a component to a section of another component in Angular? 如何从 Angular 中的另一个组件滚动到一个组件? - How to scroll to a component from another component in Angular? 我如何使用 NavLink 从组件重定向到另一个组件 - how can i use NavLink to redirect from component to another component 如何在Angular 5中从父组件继承子组件中的css样式 - How to inherit css styles in child component from parent in Angular 5 如何以角度从另一个组件销毁活动路由组件 - How to Destroy Active Routing Component from another component in angular 如何从调用另一个组件 angular 的组件发送数据 - How to send data from a component which is calling another component angular Angular:引导程序 - 如何在从其他组件调用时在组件中使用全高? - Angular : Bootstrap - How can I use full height In a component while calling from other component? 使用Angular2从Typescript更新组件模板 - Update template of component from typescript with Angular2 如何使用 Vue draggable 将项目从组件中的列表拖动到另一个组件中的列表? - How can I drag items from list in component to list inside another component using Vue draggable? 如何从 angular 7 中的其他组件调用组件 - How to call a component from other component in angular 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM