Set indeterminate on Angular checkbox

前端 未结 2 1945
轮回少年
轮回少年 2021-01-23 15:20

I\'m trying to programatically set the value of Angular checkboxes to either, false true or indeterminate. I understand that I cannot set

2条回答
  •  伪装坚强ぢ
    2021-01-23 15:45

    To make a checkbox indeterminated, you can use a directive

    import { Directive, ElementRef,Input } from '@angular/core';
    
    @Directive({ selector: '[indeterminate]' })
    export class IndeterminateDirective {
       @Input() 
       set indeterminate(value)
       {
         this.elem.nativeElement.indeterminate=value;
       }
        constructor(private elem: ElementRef) {
        }
    }
    

    Then you're checkbox can be like

    
    

    where

      click(cliente: any) {
        let indeterminated=(!cliente.checked && !cliente.indeterminated) ? true : false;
        let checked=(!cliente.checked && cliente.indeterminated)?true:false
        cliente.indeterminated = indeterminated;
        cliente.checked=checked;
      }
    

    See that you have two variables "checked" and "indeterminated", you can make a cycle as you want

提交回复
热议问题