【问题标题】:How to change style according to hidden attribute using scss?如何使用scss根据隐藏属性更改样式?
【发布时间】:2018-08-26 07:42:54
【问题描述】:

父级可复用组件:(settings-panel.component.html)

<button>
    <app-svg-icon [iconSrc]="iconSrc"></app-svg-icon>
</button>
<div [ngClass]="position" class="settings-panel" [hidden]="!panelIsVisible">
    <span class="arrow"></span>
    <ng-content></ng-content>
</div>

这是我当前的组件(outage.component.html)

 <app-settings-panel class="outage-main" iconPath="alert.svg" position="right bottom">
     <div class="outage-panel">
        <div class="outage-heading">
            <span>Notifications</span>
            <span>New</span>
        </div>      
        <span *ngFor="let outage of outageArray">
            <button type="button" class="outage-group-item">
                <span>{{outage}}</span>
            </button>
        </span>
     </div>
 </app-settings-panel>

这是我当前的组件scss(outage.component.scss)

   :host ::ng-deep app-settings-panel {

        div.settings-panel {
            right:-1px !important;
            top:none !important;
        }

        button{  
            background: #152935!important;      
            padding: 12px !important;
        }
     }

所以在这里,在我当前的组件中,我正在使用设置面板组件。所以每当hidden attributein parent div 出现在父组件中时,我需要将父按钮背景更改为红色。如果没有,我需要将父按钮的背景更改为绿色?我已经对当前组件进行了更改以执行此操作!

我怎样才能做到这一点?

我想过这样做,但是这个语法错误

//所以当div被隐藏时,我需要让按钮背景发生变化

:host ::ng-deep app-settings-panel div.settings-panel[hidden]{   
    :host ::ng-deep app-settings-panel button {
        background:red;
    }
}

如果需要更多解释,请提出建议,以便我可以更清楚地编辑。

任何帮助将不胜感激!

【问题讨论】:

  • 这有点令人困惑......你的标题提到根据hidden 属性修改css,但在你的问题中你要求open (not hidden)。为什么不改变父组件的按钮样式呢?
  • @David 当(隐藏或不隐藏)显示发生任何变化时,我需要更改样式。我不在父组件中做,因为它是一个常见的可重用组件。以上案例只针对我当前的组件。
  • 是什么导致了您的问题?更改检测或 jsut 将 css 放在哪里?
  • @David 我现在已经修改了。你现在可以检查一下吗?

标签: javascript css angular sass styles


【解决方案1】:

编辑:

在您的中断组件中,只需修改您现有的 scss

:host ::ng-deep app-settings-panel {

    div.settings-panel {
        right:-1px !important;
        top:none !important;

     &[hidden] button //Add this rule
     {
         background-color: red;
      }
    }

    button{  
        background: #152935!important;      
        padding: 12px !important;
    }
 }

只需在您的子组件中使用host

从父组件中,当 div 打开时,在 &lt;app-settings-panel 的某处设置一个 open

然后在子组件中你可以像这样设置按钮样式

:host(.open) ::ng-deep button
{
    background-color: blue;
}

【讨论】:

  • 感谢您的回复。我现在已经清楚地修改了这个问题。你能检查一下吗?
  • 我想我现在理解得更好了。你能检查更新的答案吗?
  • 嘿,谢谢!我想我们现在很接近了。但是按钮不存在于具有隐藏属性的 div 内。它位于 div 之外。你能检查更新的问题吗??
  • 从您的代码看来,它在里面。也许在 stackblitz 上创建一个简单的例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
相关资源
最近更新 更多