Angular: 如果数组中的所有值都为 false,则在模板中禁用按钮

huangapple go评论72阅读模式
英文:

Angular: Disable button in template if all values in array are false

问题

我正在寻找一种简单而高效的方法来在模板中实现这一点。

以下是代码:

studentArray = Array<boolean>;

<button class="btn btn-success" 
[disabled]="studentArray.every(value => !value)" 
(click)="generateMarksheet()">
Generate Marksheet 
</button>

如果studentArray中的每个值都为false,那么这个按钮应该保持禁用状态。我该如何实现这一点?

英文:

Im looking for a simple yet efficient way to achieve this in template.

The code is as follows:

studentArray = Array&lt;boolean&gt;;

&lt;button class=&quot;btn btn-success&quot; 
[disabled]=&quot;&quot; 
click=&quot;generateMarksheet()&quot;&gt;
Generate Marksheet 
&lt;/button&gt;

This button should remain disabled if every value in studentArray is false. How can I achieve this?

答案1

得分: 1

token发送数量= 108,token接受数量= 42
分析:这段代码中有2句交互语言,
思考:其中部分交互语言只要汉化1个单词
计划:开始汉化从"In my opinion"开始的内容:

在我看来,实现这一目标的最简单方式是在您的ts文件中创建一个方法,该方法将检查所有的值是否为假。

例如:

private youArr: boolean[];

public areAllValuesFalse(): boolean {
  return this.youArr.every(element => element === false)
}

然后,您可以在模板中绑定这个方法:

<button class="btn btn-success" 
   [disabled]="areAllValuesFalse()" 
   click="generateMarksheet()">
   生成成绩单 
</button>

注意: 如果您想检查假值,您可以使用!element而不是element === false

英文:

In my opinion the easiest way to achieve this is to create a method in your ts file that will check if all the values are false.

For example :

private youArr: boolean[];

public areAllValuesFalse(): boolean {
  return this.youArr.every(element =&gt; element === false)
}

You can then bind the method in your template :

&lt;button class=&quot;btn btn-success&quot; 
   [disabled]=&quot;areAllValuesFalse()&quot; 
   click=&quot;generateMarksheet()&quot;&gt;
   Generate Marksheet 
&lt;/button&gt;

Note : If you want to check for falsy values, you can then use !element instead of element === false

答案2

得分: 0

token发送数量= 2,token接受数量= 1
分析:这段代码中有 1 句交互语言,
思考:其中 "<button class="btn btn-success" [disabled]="!studentArray.includes(true)" click="generateMarksheet()">" 为程序代码,不能汉化
推测:部分交互语言只要汉化 "If you want to do it in the Template it could look like this: Generate Marksheet"
计划:开始汉化从 "If you want to do it in the Template it could look like this: Generate Marksheet"
scss 适度汉化交互语言的代码:如果你想在模板中这样做:生成成绩单

英文:

If you want to do it in the Template it could look like this:

&lt;button class=&quot;btn btn-success&quot; 
   [disabled]=&quot;!studentArray.includes(true)&quot; 
   click=&quot;generateMarksheet()&quot;&gt;
   Generate Marksheet 
&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年7月27日 20:40:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定