1

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

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

4

3 回答 3

9

您可以通过使用的value属性MatCheckbox和监听更改来实现这一点。例如:

HTML

<mat-checkbox [value]="falseValue" (change)="checkboxChange($event.source, $event.checked)">
  Check me!
</mat-checkbox>

TS

falseValue = 'No'
trueValue = 'Yes';

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

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

TS

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

用法:

<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

于 2019-01-11T18:47:40.580 回答
1

我使用了类似于上面评论的方法。将几个复选框的值保存在一个名为的数组中filtroStatus

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 ; 
    }
}
于 2020-02-28T16:51:47.630 回答
0

对于寻找简单答案的人

TS:

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:

<mat-checkbox appCheckboxValue trueValue="ACTIVE" falseValue="INACTIVE" [(ngModel)]="myCheck">
</mat-checkbox> 

于 2021-03-10T05:07:50.933 回答