首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为mat-checkbox分配自定义值?

如何为mat-checkbox分配自定义值?
EN

Stack Overflow用户
提问于 2019-01-11 00:52:35
回答 3查看 7.5K关注 0票数 1

我想要附加我的状态字段与mat-checkbox,并希望以字符串的形式获取值,就像我们在Material-1中使用的那样。

我正在寻找Material-7中ng-true-value="'ACTIVE'" ng-false-value="'INACTIVE'"的替代品。

EN

回答 3

Stack Overflow用户

发布于 2019-01-12 02:47:41

您可以通过使用MatCheckboxvalue属性并侦听更改来实现这一点。例如:

HTML

代码语言:javascript
复制
<mat-checkbox [value]="falseValue" (change)="checkboxChange($event.source, $event.checked)">
  Check me!
</mat-checkbox>

TS

代码语言:javascript
复制
falseValue = 'No'
trueValue = 'Yes';

checkboxChange(checkbox: MatCheckbox, checked: boolean) {
  checkbox.value = checked ? this.trueValue : this.falseValue;
}

您也可以将其实现为指令:

TS

代码语言:javascript
复制
import {Directive, Input} from '@angular/core';
import {MatCheckbox} from '@angular/material/checkbox';

@Directive({
  selector: 'mat-checkbox[checkboxValue]',
  exportAs: 'checkboxValue'
})
export class CheckboxValueDirective {

  @Input('checkboxValue') checkbox: MatCheckbox;
  @Input() falseValue: string = '0';
  @Input() trueValue: string = '1';

  ngOnInit() {
    this.checkbox.value = this.checkbox.checked ? this.trueValue : this.falseValue;
    this.checkbox.registerOnChange((checked: boolean) => {
      this.checkbox.value = checked ? this.trueValue : this.falseValue;
    })
  }
}

用法:

代码语言:javascript
复制
<mat-checkbox #checkbox [checkboxValue]="checkbox" trueValue="Yes" falseValue="No" [checked]="true">
  Check me!
</mat-checkbox> 

以下是StackBlitz上的指令示例:https://stackblitz.com/edit/angular-aapsr6?file=app/checkbox-value-directive.ts

票数 9
EN

Stack Overflow用户

发布于 2020-02-29 00:51:48

我使用了与上面的注释类似的方法。在名为filtroStatus的数组中保存多个复选框的值​​

代码语言:javascript
复制
filtroStatus: string[] = [];    
checkboxChange(checkbox: MatCheckbox, checked: boolean, statusValue: string){
  if (checked) { 
    this.filtroStatus.push(statusValue);
    statusValue === 'em_analise' ? this.filtroStatus.push('pendente') : null;
    statusValue === 'em_dia' ? this.filtroStatus.push('em_andamento') : null;
    } else {
      const index = this.filtroStatus.indexOf(statusValue);
      const total_excluir = statusValue ==='em_analise' || 'em_dia' ? 2 : 1;

      index >= 0 ? this.filtroStatus.splice(index, total_excluir) : null ; 
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-03-10 13:07:51

对于正在寻找简单答案的人来说

TS:

代码语言:javascript
复制
import { Directive, Input } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { MatCheckbox } from '@angular/material/checkbox';

@Directive({
  selector: 'mat-checkbox[appCheckboxValue]'
})
export class CheckboxValueDirective implements ControlValueAccessor {
  @Input() trueValue = true;
  @Input() falseValue = false;

  constructor(@Optional() @Self() private ngControl: NgControl, private checkbox: MatCheckbox) {
    if (this.ngControl) {
      this.ngControl.valueAccessor = this;
    }
  }

  writeValue(value: any): void {
    this.checkbox.writeValue(value === this.trueValue);
  }

  registerOnChange(fn: any): void {
    this.checkbox.registerOnChange((checked: boolean) => {
      fn(checked ? this.trueValue : this.falseValue);
    });
  }

  registerOnTouched(fn: any): void {
    this.checkbox.registerOnTouched(fn);
  }

  setDisabledState?(isDisabled: boolean): void {
    this.checkbox.setDisabledState(isDisabled);
  }
}

HTML:

代码语言:javascript
复制
<mat-checkbox appCheckboxValue trueValue="ACTIVE" falseValue="INACTIVE" [(ngModel)]="myCheck">
</mat-checkbox> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54133427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档