【问题标题】:Can we use a method on [checked] property of checkbox to set it to true/false我们可以在复选框的 [checked] 属性上使用方法将其设置为 true/false
【发布时间】:2020-04-30 13:24:30
【问题描述】:

我想在方法调用上设置复选框的 [checked] 属性,但是当我这样做时,会调用该方法但不会更改复选框的状态

HTML

<div*ngFor="let vest_style of VEST_STYLE">
    <input type="checkbox" value="{{vest_style._id}}" (change)="onCheckboxChange($event,i)" 
    [checked]="isCheckedFromOrder(vest_style._id)">&nbsp;{{vest_style.name}}
</div>

TS

isCheckedFromOrder(id) {
    this.styles.value.forEach(style => {
      console.log(style === id)
      return style === id
    })
  }

【问题讨论】:

  • 你在 oncheckchange 事件处理程序中更改 this.styles.value 吗?
  • 你能把代码加到问题里吗?
  • 是的,我正在填充 this.styles 这是一个表单数组,通过事件处理程序检查项目。
  • 但是这里 this.styles.value 是从后端预填充的,如果后端的值与 id 匹配,只需将 checked 属性设置为 true。
  • 也许样式和 id 不是三等号,尝试只使用双等号 '==' 而不是 '==='。此外,console.log 是否具有您所期望的正确布尔值?

标签: html angular typescript checkbox checked


【解决方案1】:

我知道问题是什么。使用以下代码代替您的代码。

let checked = false;
this.style.value.forEach(style => {
if (style == id) checked = true;
});
return checked;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多