gpt4 book ai didi

angular - 通过 bool 值选中和取消选中 Angular 2 中的复选框

转载 作者:太空狗 更新时间:2023-10-29 17:54:28 28 4
gpt4 key购买 nike

我想让 Angular 2 中的复选框自动选中或取消选中。当值为 1 时,复选框将选中,而此值为 0 时,复选框将自动取消选中。

public LightControl() {
this.dataLight = this._LightService.AutoLightController(this._ConnectService.SocketInstanse())
.subscribe(data => {
this.temp = JSON.stringify(data['status']);
})
}

HTML:

 <div class="togglebutton">
<label> <input type="checkbox" [checked]="temp(change)="temp">Light</label>
</div>
  • 当 temp 值为 true 时它会检查,但当 temp 值为 false 时,它​​不会自动取消选中。

最佳答案

使用输入字段的选中属性。将 bool 变量绑定(bind)到输入复选框的选中属性。使用可观察的概念,并订阅将触发输入字段复选框可见性的数据变量。[checked]=‘yourModel.isChecked’在 subscribe 方法中,将当前状态分配给 yourModel 的这个 isChecked 属性。并且根据该属性的当前值,复选框将被选中或取消选中。

observableVariable。订阅( data => {处理数据或根据处理后的数据,分配状态给

yourModel.isChecked = status

});

关于angular - 通过 bool 值选中和取消选中 Angular 2 中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44087023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com