Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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 反转mat复选框的“选中”值_Angular_Angular7_Angular Material2_Angular Material 7 - Fatal编程技术网

Angular 反转mat复选框的“选中”值

Angular 反转mat复选框的“选中”值,angular,angular7,angular-material2,angular-material-7,Angular,Angular7,Angular Material2,Angular Material 7,选中“角度材质垫”复选框时,其值为true。未选中时,其值为false 有没有办法扭转这种行为?我需要的正好相反。调用this.FilterPerformGroup.getRawValue时,选中的复选框应序列化为false,未选中的复选框应序列化为true 我希望有这样的事情: 或者我是否需要创建一个自定义指令,如下所示: 我的目标是实现以下代码: this.filterGroup=新表单组{ 已解决:新FormControl, }; this.filterGroup.getRawValue;

选中“角度材质垫”复选框时,其值为true。未选中时,其值为false

有没有办法扭转这种行为?我需要的正好相反。调用this.FilterPerformGroup.getRawValue时,选中的复选框应序列化为false,未选中的复选框应序列化为true

我希望有这样的事情:

或者我是否需要创建一个自定义指令,如下所示:

我的目标是实现以下代码:

this.filterGroup=新表单组{ 已解决:新FormControl, }; this.filterGroup.getRawValue;
选中复选框时返回{resolved:false}。

您可以编写自定义管道,将接收到的值更改为您希望的方式。
与接收到的值为true类似,它将以所需的方式转换该值,而在您的情况下该方式为false。

Tobias,即使您没有输入,也存在formControl。所以,如果你有

表单=新表单组{ 已解决:新窗体控件 } 您可以使用以下内容:

检查我! 看中

注意,如果您只有唯一的formControl,则:

mycontrol=新表单控件 你用

检查我!
如果有人想在没有FormControl的情况下执行此操作,最简单的解决方案是将值输入绑定到checked属性:

<mat-checkbox #checkbox [value]="!checkbox.checked">
  {{ checkbox.value }}
</mat-checkbox>
我使用了的答案,并为API设置为false或null:

断然的 在组件中:

this.resolvedFormControl=新FormControlnull; this.resolvedFormControl.setValuefalse.toString; this.filterGroup=新表单组{ 已解决:this.resolvedFormControl, }; this.filterGroup.valueChanges.subscribe=>{ console.logthis.filterGroup.getRawValue; //已解析为“false”或null }
我不确定你认为管子可以插在哪里。你能添加你的解决方案的一个片段吗?我使用了一个变量测试,它初始化为true,我使用反转管道将值转换为False,以备不时之需@tobias47n9e@VaibhavKaushik,最好在答案中写下代码,不要在stackblitz中添加链接或在答案中添加链接。此stackblitz可以消失或更改,并且答案不完整您不需要绑定[checked],除非您希望检查初始状态,但您应该在FormControl上设置初始true/unchecked值。