Ada*_*dam 23 conditional styling angular
在Angular 2中,我在TABLE TD中绑定了这样的美元值.
<td>
{{eachOutlet.dollarValue}}
</td>
Run Code Online (Sandbox Code Playgroud)
此dollarValue将小于0或等于0或大于0.当它小于零时,它应显示为"红色"颜色.当它为零时,什么都不应该出现.只是空白文本.当它大于零时,它应该使用千位分隔符并显示数字.
如何使用Angular 2绑定应用此类条件样式?它甚至可以做到吗?
Gün*_*uer 51
<td>
<span
*ngIf="eachOutlet.dollarValue != 0"
[style.color]="eachOutlet.dollarValue < 0 ? 'red' : null">
{{eachOutlet.dollarValue | number:'1.0-2'}}
</span>
</td>
Run Code Online (Sandbox Code Playgroud)
您还可以创建一个执行样式(number管道除外)的指令,以便更容易地重用不同的元素.
多个样式属性的另一种选择:
<div style="word-break: break-all;" [ngStyle]="{ 'top': getTop() +'px', 'left': getLeft() +'px' }"> </div>
Run Code Online (Sandbox Code Playgroud)
getTop(){
return (this.topValueShowComment> 0)? this.topValueShowComment : 0;
}
getLeft(){
return (this.leftValueShowComment> 0)? this.leftValueShowComment : 0;
}
Run Code Online (Sandbox Code Playgroud)