Angular 4 checkbox change value

后端 未结 9 2059
臣服心动
臣服心动 2020-12-03 13:30

how could you achieve in Angular 4 that when you register in a checkbox save an \"A\" or \"B\" value. As much as I try, he is only sending me true or false, I hope someone c

相关标签:
9条回答
  • 2020-12-03 13:52
    changed = (evt) => {    
    this.isChecked = evt.target.checked;
    }
    
    <input type="checkbox" [checked]="checkbox" (change)="changed($event)" id="no"/>
    
    0 讨论(0)
  • 2020-12-03 13:53

    Another approach is to use ngModelChange:

    Template:

    <input type="checkbox" ngModel (ngModelChange)="onChecked(obj, $event)" />
    

    Controller:

    onChecked(obj: any, isChecked: boolean){
      console.log(obj, isChecked); // {}, true || false
    }
    
    

    I prefer this method because here you get the relevant object and true/false values of a checkbox.

    0 讨论(0)
  • 2020-12-03 13:55

    Inside your component class:

    checkValue(event: any) {
      this.userForm.patchValue({
        state: event
      })
    }
    

    Now in controls you have value A or B

    0 讨论(0)
提交回复
热议问题