启用已禁用的切换​​按钮/更改json值的角度

时间:2018-08-16 07:18:06

标签: json angular toggle

我想创建一个切换按钮,用于更改Dom中的json布尔值。我想禁用或启用div

check: boolean = true;
  
  
  checkStatus(status) {
    if(status) {
      this.check = false;
    } else {
      this.check = true;
    }
    console.log(status);
  }
{
        id: 1,
        name: 'PV Panels',
        currentP: '100 kW',
        setpp: '100 kW',
        currentq: '2 kVar',
        setq: '2 kVar',
        required: true

    },
    
    
    HTML
    
      <label>
      <td ><input [ngModel]="test" [checked]="check" type="checkbox" (change) = "checkStatus(status)"></td>
</label>

1 个答案:

答案 0 :(得分:0)

尝试一下:

https://stackblitz.com/edit/angular-5xyuaq?file=src%2Fapp%2Fapp.component.ts

TS文件

check: boolean = false;

  checkStatus() {
    this.check = !this.check;
    this.json.required = this.check;
  }

  json = {
        id: 1,
        name: 'PV Panels',
        currentP: '100 kW',
        setpp: '100 kW',
        currentq: '2 kVar',
        setq: '2 kVar',
        required: this.check
  }

HTML文件

<label>
  <td >
    <input [checked]="this.check"       
    type="checkbox" (change) = "this.checkStatus()">
  </td>
</label>

<div *ngIf="check">
  DIV TO SHOW OR HIDE
</div>

{{ this.json | json }}