Set indeterminate on Angular checkbox

前端 未结 2 1947
轮回少年
轮回少年 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 16:05

    Yes, you can use [indeterminate]="condition" (without any directives). You can't do indeterminate="{{condition}}". The difference is that [...] sets property of the element, while without brackets you are setting attribute. HTMLInput element has property indeterminate, but doesn't have such attribute.

    In order use ngModel or FormControl, which holds simultaneously the both checked and indeterminate states, you should create a custom directive or/and additional logic in your primary component (for reference, see the answer from @Eliseo).

    Q1. How can I cycle through these three state in the same way as you can with gmail?

    Handle clicks, change state of the pair "checked" and "indeterminate" in cycle. Example with jQuery.

    Q2. Why am I getting the ExpressionChangedAfterItHasBeenCheckedError with the current setup?

    You have side effect in the checkedLabels method, which changes state of the component, each time Angular checks value of the expression [indeterminate]="checkedLabels()".

提交回复
热议问题