选中Angular 5,HTML,boolean on复选框

Mr.*_*ast 36 html typescript angular

Angular 5,Typescript 2.7.1

我似乎无法在返回布尔值时检查复选框,我试过,item.check返回true或false.

<tr class="even" *ngFor="let item of rows">
<input value="{{item.check}}" type="checkbox" checked="item.check">
Run Code Online (Sandbox Code Playgroud)

在输入内部写入选中时,始终选中该复选框.并且它不会被取消选中checked="false".

相反,有更好的方法来使用Angular功能吗?像ngModel或ngIf ???

<input type="checkbox" [checked]="item.check == 'true'">
Run Code Online (Sandbox Code Playgroud)

小智 64

尝试:

[checked]="item.checked"
Run Code Online (Sandbox Code Playgroud)

检查:如何处理Angular中的不同表单控件

  • 谢谢,我已经尝试过,但是我需要添加&lt;input type =“ checkbox” [checked] =“ item.check =='true'”&gt;然后它起作用了,谢谢 (2认同)
  • 那是因为您的item.check是String而不是Boolean item.check = true; // boolean item.check ='true'; //串 (2认同)

Col*_*ini 11

当您拥有对象的副本时,该[checked]属性可能不起作用,在这种情况下,您可以这样使用(change)

<input type="checkbox" [checked]="item.selected" (change)="item.selected = !item.selected">
Run Code Online (Sandbox Code Playgroud)


Abd*_*zad 10

您可以使用此:

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

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

  • 如何从 ts 组件中的 $event 中提取布尔值? (2认同)
  • @svichkar提取时必须使用** event.target.checked **值。如果为true,则选中复选框,否则取消选中。 (2认同)

Jan*_*ana 7

希望这将有助于有人开发具有自定义样式的自定义复选框组件。该解决方案也可以与表格一起使用。

的HTML

<label class="lbl">

  <input #inputEl type="checkbox" [name]="label" [(ngModel)]="isChecked" (change)="onChange(inputEl.checked)"
   *ngIf="isChecked" checked>
  <input #inputEl type="checkbox" [name]="label" [(ngModel)]="isChecked" (change)="onChange(inputEl.checked)"
   *ngIf="!isChecked" >
  <span class="chk-box {{isChecked ? 'chk':''}}"></span>
  <span class="lbl-txt" *ngIf="label" >{{label}}</span>
</label>
Run Code Online (Sandbox Code Playgroud)

checkbox.component.ts

    import { Component, Input, EventEmitter, Output, forwardRef, HostListener } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

const noop = () => {
};

export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => CheckboxComponent),
  multi: true
};

/** Custom check box  */
@Component({
  selector: 'app-checkbox',
  templateUrl: './checkbox.component.html',
  styleUrls: ['./checkbox.component.scss'],
  providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class CheckboxComponent implements ControlValueAccessor {


  @Input() label: string;
  @Input() isChecked = false;
  @Input() disabled = false;
  @Output() getChange = new EventEmitter();
  @Input() className: string;

  // get accessor
  get value(): any {
    return this.isChecked;
  }

  // set accessor including call the onchange callback
  set value(value: any) {
    this.isChecked = value;
  }

  private onTouchedCallback: () => void = noop;
  private onChangeCallback: (_: any) => void = noop;


  writeValue(value: any): void {
    if (value !== this.isChecked) {
      this.isChecked = value;
    }
  }

  onChange(isChecked) {
    this.value = isChecked;
    this.getChange.emit(this.isChecked);
    this.onChangeCallback(this.value);
  }

  // From ControlValueAccessor interface
  registerOnChange(fn: any) {
    this.onChangeCallback = fn;
  }

  // From ControlValueAccessor interface
  registerOnTouched(fn: any) {
    this.onTouchedCallback = fn;
  }

  setDisabledState?(isDisabled: boolean): void {

  }

}
Run Code Online (Sandbox Code Playgroud)

checkbox.component.scss

   @import "../../../assets/scss/_variables";
/* CHECKBOX */

.lbl {
    font-size: 12px;
    color: #282828;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    cursor: pointer;
    &.checked {
        font-weight: 600;
    }
    &.focus {
      .chk-box{
        border: 1px solid #a8a8a8;
        &.chk{
          border: none;
        }
      }
    }
    input {
        display: none;
    }

    /* checkbox icon */
    .chk-box {
        display: block;
        min-width: 15px;
        min-height: 15px;
        background: url('/assets/i/checkbox-not-selected.svg');
        background-size: 15px 15px;
        margin-right: 10px;
    }
    input:checked+.chk-box {
        background: url('/assets/i/checkbox-selected.svg');
        background-size: 15px 15px;
    }
    .lbl-txt {
        margin-top: 0px;
    } 

}
Run Code Online (Sandbox Code Playgroud)

用法

外部形式

<app-checkbox [label]="'Example'" [isChecked]="true"></app-checkbox>
Run Code Online (Sandbox Code Playgroud)

内部表格

<app-checkbox [label]="'Type 0'" formControlName="Type1"></app-checkbox>
Run Code Online (Sandbox Code Playgroud)