在 Angular 中使用"style."语法对子元素进行样式绑定



是否可以使用Angular中的style.语法将任何样式应用于子组件(我正在运行Angular 8(。例如:

<div class="col-8 kt-padding-l-10" [style.fill]="visitor | vcolor">
<div>{{ visitor?.id?.substr(0,6) }}</div>
<div>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1" class="kt-svg-icon">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" >
<rect x="0" y="0" width="24" height="24"/>
<path d="M21,12.0829584 C20.6747915,12.0283988 20.3407122,12 20,12 C16.6862915,12 14,14.6862915 14,18 C14,18.3407122 14.0283988,18.6747915 14.0829584,19 L5,19 C3.8954305,19 3,18.1045695 3,17 L3,8 C3,6.8954305 3.8954305,6 5,6 L19,6 C20.1045695,6 21,6.8954305 21,8 L21,12.0829584 Z M18.1444251,7.83964668 L12,11.1481833 L5.85557487,7.83964668 C5.4908718,7.6432681 5.03602525,7.77972206 4.83964668,8.14442513 C4.6432681,8.5091282 4.77972206,8.96397475 5.14442513,9.16035332 L11.6444251,12.6603533 C11.8664074,12.7798822 12.1335926,12.7798822 12.3555749,12.6603533 L18.8555749,9.16035332 C19.2202779,8.96397475 19.3567319,8.5091282 19.1603533,8.14442513 C18.9639747,7.77972206 18.5091282,7.6432681 18.1444251,7.83964668 Z" fill="#00000" />
<circle fill="#00000" opacity="0.3" cx="19.5" cy="17.5" r="2.5"/>
</g>
</svg>
yehia@salam.com
</div>

我想要的是为g元素下具有fill属性的所有元素设置样式,如果我在纯css中这样做,它将是:

g [fill]{
fill: red; /* or in my case the visitor variable with the vcolor pipe */
}

看起来我做不到,但要仔细检查,另一个解决方案是为每个组件添加[style.fill]="visitor | vcolor",如circlepath,但这是非常多余的。我有什么选择?创建一个单独的组件也是最后的手段。

您必须使用:ng-deep来穿透子组件的样式并应用它们,因此在该组件的scss中,尝试将其添加到顶部:

:host { // host to say for this component
&::ng-deep {
g[fill] {
fill: red;
}
}
}

你可能需要另一个::ng-deep,但这肯定会让你开始。

最新更新