Angular 4 checkbox change value

后端 未结 9 2058
臣服心动
臣服心动 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:32

    I am guessing that this is what something you are trying to achieve.

    <input type="checkbox" value="a" (click)="click($event)">A
    <input type="checkbox" value="b" (click)="click($event)">B
    
    click(ev){
       console.log(ev.target.defaultValue);
    }
    
    0 讨论(0)
  • 2020-12-03 13:33

    This it what you are looking for:

    <input type="checkbox" [(ngModel)]="isChecked" (change)="checkValue(isChecked?'A':'B')" />
    

    Inside your class:

    checkValue(event: any){
       console.log(event);
    }
    

    Also include FormsModule in app.module.ts to make ngModel work !

    Hope it Helps!

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

    You can use this:

    <input type="checkbox" [checked]="record.status" (change)="changeStatus(record.id,$event)">
    

    And on your ts file,

    changeStatus(id, e) {
        var status = e.target.checked;
        this.yourseverice.changeStatus(id, status).subscribe(result => {
            if (status)
                this.notify.success(this.l('AddedAsKeyPeople'));
            else
                this.notify.success(this.l('RemovedFromKeyPeople'));
    
        });
    }
    

    Here, record is the model for current row and status is boolean value.

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

    try this worked for me :

    checkValue(event: any) {
        this.siteSelected.majeur = event;
    }
    
    0 讨论(0)
  • 2020-12-03 13:50

    Give a try on this,

    Template

    <input (change)="FieldsChange($event)" value="angular" type="checkbox"/>
    

    Ts File

    FieldsChange(values:any){
    console.log(values.currentTarget.checked);
    }
    
    0 讨论(0)
  • 2020-12-03 13:50

    This works for me, angular 9:

    component.html file:

    <mat-checkbox (change)="checkValue($event)">text</mat-checkbox>
    

    component.ts file:

    checkValue(e){console.log(e.target.checked)}
    
    0 讨论(0)
提交回复
热议问题