Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度4复选框更改值_Angular_Typescript - Fatal编程技术网

Angular 角度4复选框更改值

Angular 角度4复选框更改值,angular,typescript,Angular,Typescript,在Angular 4中,当您在复选框中注册时,如何实现保存“a”或“B”值。虽然我尽了最大努力,但他给我的信是真是假,我希望有人能帮助我 registry.component.ts this.userForm = new FormGroup({ state: new FormControl('',), }); registry.component.html <div class="form-group"> <label>State&

在Angular 4中,当您在复选框中注册时,如何实现保存“a”或“B”值。虽然我尽了最大努力,但他给我的信是真是假,我希望有人能帮助我

registry.component.ts

  this.userForm = new FormGroup({
   state: new FormControl('',),
  });
registry.component.html

<div class="form-group">
  <label>State</label>
  <input type="checkbox"
         [(ngModel)]="isChecked"
         (change)="checkValue(isChecked?'A':'B')"
         formControlName="state"/>
</div>  

<pre>{{userForm.value | json}}</pre>

陈述
{{userForm.value | json}}

这样我可以让控制台显示我想要的值(A或B),但JSON中的值仍然是真或假。

我猜这就是您试图实现的目标

<input (change)="fieldsChange($event)" value="angular" type="checkbox"/>
A
B
点击(ev){
console.log(ev.target.defaultValue);
}
试试这个

模板

fieldsChange(values:any):void {
  console.log(values.currentTarget.checked);
}
changed = (evt) => {    
this.isChecked = evt.target.checked;
}

<input type="checkbox" [checked]="checkbox" (change)="changed($event)" id="no"/>

这就是您想要的:


在课堂上:

checkValue(事件:任意){
console.log(事件);
}
还应在
app.module.ts
中包含FormsModule,以使ngModel正常工作

希望有帮助

changed=(evt)=>{
<input type="checkbox" [checked]="record.status" (change)="changeStatus(record.id,$event)">
this.isChecked=evt.target.checked; }
您可以使用以下功能:

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'));

    });
}

这里,记录是当前行的模型,状态是布尔值。

另一种方法是使用
ngModelChange

模板:

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

我更喜欢这种方法,因为在这里你可以得到相关的对象和复选框的值。

试试这个对我有用的方法:


在组件类中:

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

现在,在控件中有值A或B,这对我来说是可行的,角度9:

component.html文件:

checkValue(e){console.log(e.target.checked);}

通过您的回答,我可以看到控制台(“A”、“B”)中的值发生了变化,但我必须补充的是,在注册表中,我发送了一个JSON,在FormGroup和FormControl中发送了一个agrunpando,在JSON中,它继续发送true或false,关于如何解决这个问题的任何帮助?您有办法解析它吗?我不明白你现在的问题!简单易用的解决方案,但在我的浏览器控制台中,它说“看起来您在与formControlName相同的表单字段上使用ngModel。对使用ngModel输入属性和ngModelChange事件以及反应式表单指令的支持在Angular v6中已被弃用,并将在Angular v7中删除。”但它仍在发挥作用。它能用很长时间吗?你能解释一下吗?这对我很有用:
checkValue(event:any){
console.log(event.currentTarget.checked);
}
请不要把代码块作为答案转储。一个好的答案解释了解决方案并给出了一些上下文。$event.target.checked是我所寻找的,而不是$event.target.value作为一个救命的答案。谢谢:)就像一个charmExact的答案,非常感谢。我需要使用
e.target。选中
,这样才能工作。
checkValue(event: any) {
    this.siteSelected.majeur = event;
}
checkValue(event: any) {
  this.userForm.patchValue({
    state: event
  })
}
<mat-checkbox (change)="checkValue($event)">text</mat-checkbox>
checkValue(e){console.log(e.target.checked);}