简体   繁体   English

带条件的角度属性绑定

[英]Angular Property Binding with Condition

How can I add a + 1 to my condition with [class.active] binding? 如何通过[class.active]绑定为条件添加+ 1 It isn't working in this way... 它不以这种方式工作...

[class.active]="userProfile.id === (userProfile.lastId + 1)"

Use userProfile['lastId'] + 1 使用userProfile['lastId'] + 1

Try: 尝试:

[class.active]="userProfile.id === userProfile['lastId']  + 1)"

Your code is working fine check stackblitz example below or you can use ngClass 您的代码在下面的示例中可以很好地检查stackblitz示例,或者您可以使用ngClass

[ngClass]="{'active': userProfile.id == (userProfile.lastId + 1)}"

Check if peroperties that you'r using are of number typo may be they are evaluating to string like id:'1' 检查您使用的属性是否为数字错字,是否可能是评估为id:'1'之类的字符串

Stackblitz Stackblitz

尝试这个

[ngClass]="{'active': userProfile.id === userProfile.lastId + 1}"

you can uns == if any of the proprty are not number (string value) , drop the type checking 您可以uns ==如果任何属性都不是数字(字符串值),请取消类型检查

<p [class.active]="+userProfile.id == (+userProfile.lastId + 1)" >...</p>

the + infornt of userProfile.lastId,userProfile.id will convert the value to number userProfile.lastId,userProfile.id+信息会将值转换为数字

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

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