简体   繁体   English

Angular 2 ng-if失败

[英]Angular 2 ng-if fails

Trying to apply css styles dynamically for the element which has ng-if condition. 尝试为具有ng-if条件的元素动态应用CSS样式。 Works fine, if the condition is true. 如果条件为真,则工作正常。 Is there any way that I can modify the element even if the condition fails. 即使条件失败,我有什么方法可以修改元素。 I know I can find the element (by getElementsByClassName) and modify but is there any other better solution other than this? 我知道我可以找到该元素(通过getElementsByClassName)并进行修改,但是除此以外还有其他更好的解决方案吗?

ex: 例如:

<span *ngIf="orderBy=='asc'" [ngStyle]="{'height': value+ '%'}"></span>                              
    <span *ngIf="orderBy=='desc'" [ngStyle]="{'height': value+ '%'}"></span>

You can use ngClass for that. 您可以为此使用ngClass。 Add class based on the conditional value. 根据条件值添加类。

[ngClass]="{orderBy=='asc'? 'someclass': 'otherclass'}"

You can write seperate styles for each of the class. 您可以为每个类编写不同的样式。

You can use ng-class to achieve this 您可以使用ng-class实现此目的

ng-class="{' classname ':orderBy==='asc', ' classname ':orderBy==='desc'}" ng-class =“ {' classname ':orderBy ==='asc',' classname ':orderBy ==='desc'}”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM