[英]Shorthand for variable assignment based on OR expression
Is there a shorthand for the assigning of a variable to an OR expression? 将变量分配给OR表达式是否有捷径? For example, I have a button on my UI that is disabled depending on the state of some variables.
例如,我的UI上有一个按钮,该按钮根据某些变量的状态而被禁用。
this.disableButton = data && data.items.length < 2 && pending;
I also have a checkbox that determines whether or not said button will be disabled or enabled. 我还有一个复选框,用于确定是否要禁用或启用所述按钮。
So, the button finally is disabled/enabled based on an expression that looks like this: 因此,该按钮最终基于如下所示的表达式被禁用/启用:
this.disableButton = this.disableButton || !checked
And on my template, I have something like: 在我的模板上,我有如下内容:
<component [class.disabled]="disableButton"></component>
I was just curious if there were a way to shorten the syntax of the above expression. 我只是好奇是否有一种方法可以缩短上述表达式的语法。 Something like:
就像是:
this.disableButton ||= !checked
Thanks 谢谢
It would be more straightforward to assign to disableButton
all at once rather than assign an intermediate value to the result first and then reassign later - that's confusing. 一次全部分配
disableButton
而不是先为结果分配中间值,然后再重新分配,这会更直接-这很令人困惑。 Because checked
alone can determine the result, it'll be clearest if you examine it first: 因为单独
checked
可以确定结果,所以如果先检查一下,它将是最清晰的:
this.disableButton = !checked || (data && data.items.length < 2 && pending);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.