[英]Pass Angular component input parameter to child div for CSS
I would like to set CSS based on an input on the div inside my component.我想根据组件内 div 上的输入设置 CSS 。
I pass parameters to my custom component with @Input and I would like to pass one of the parameters to the html like this:我使用@Input 将参数传递给我的自定义组件,我想将其中一个参数传递给 html,如下所示:
the title works but the titletype I have no idea about标题有效,但标题类型我不知道
My component html:我的组件 html:
<ion-input placeholder='Label'>
<div class=label titletype= **the parameter from my .ts file**>
{{title}}
</div>
</ion-input>
And my CSS还有我的 CSS
.label[titletype=inborder] {
padding: 4px;
position: absolute;
color: $textColor;
margin-left: 13px;
font-size: 12px;
line-height: 16px;
margin-top: -20px;
background: white;
}
.label[titletype=corner] {
padding: 4px;
position: absolute;
color: $textColor;
float: right;
font-size: 12px;
line-height: 16px;
right:5px;
bottom: -24px;
}
You could have a better dev experience using NgClass :您可以使用NgClass获得更好的开发体验:
<ion-input placeholder='Label'>
<div [ngClass]="{'label-titletype-corner': yourProperty === 'corner', 'label-titletype-inborder': yourProperty === 'inborder', ...and so on}>
{{title}}
</div>
</ion-input>
with a simple adjustment to the ccs declarations as well:对 ccs 声明也进行了简单的调整:
.label[titletype=inborder], ,label-titletype-inborder {
...
.label[titletype=corner], .label-titletype-corner {
...
You can do it by class您可以通过 class 来完成
.inborder{
padding: 4px;
position: absolute;
color: $textColor;
margin-left: 13px;
font-size: 12px;
line-height: 16px;
margin-top: -20px;
background: white;
}
.corner{
padding: 4px;
position: absolute;
color: $textColor;
float: right;
font-size: 12px;
line-height: 16px;
right:5px;
bottom: -24px;
}
Then based on the condition you can set the class in the div with title like this然后根据条件,您可以在标题如下的 div 中设置 class
<ion-input placeholder='Label'>
<div class="label" [ngClass]="{'inborder': titletype=='inborder', 'corner': titletype=='corner'}">
{{title}}
</div>
</ion-input>
This would apply the style based on the input in the div if it is as这将根据 div 中的输入应用样式,如果它是
@Input() titletype:string;
Let's say that the @Input() property of your custom component is: @Input() myProp: string;假设您的自定义组件的 @Input() 属性是: @Input() myProp: string;
So in the html you can use interpolation.所以在 html 中可以使用插值。
<ion-input placeholder='Label'>
<div class=label titletype={{ this.myProp }}>
{{title}}
</div>
</ion-input>
Also, if your @Input it's object instead of a string, you can evaluate his property to determinate which titletype you should show.此外,如果您的 @Input 是 object 而不是字符串,您可以评估他的属性以确定您应该显示的标题类型。
Example:例子:
@Input() myObj: CustomClass; @Input() myObj: 自定义类;
html html
<ion-input placeholder='Label'>
<div class=label titletype={{ this.myObj.myProp === condition ? 'inborder' : 'corner' }}>
{{title}}
</div>
</ion-input>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.