Trevortni
Trevortni

Reputation: 738

Accessing MatCheckbox from AbstractControl

I have a MatCheckbox in a FormGroup that I want to be able to read the indeterminate value of. But the FormGroup only gives me AbstractControls, which do not seem be castable to, nor contain any reference to, the underlying control that I have been able to find.

How can I read the indeterminate value of the underlying MatCheckbox?

Edit: There are two .ts files that interact with my form: the first is the controller (I think that's the right term?) for the form, that has access to variables on the form, and has functions that can be called from the html. The second is a service that appears to act as a go-between between the form and other services/components. The functions in this one are also reachable from the form, but it does not appear to be able to touch the form itself; it is in this service that I am handed the group of AbstractControl, but I haven't quite been able to figure out how (this wasn't originally written by me). This service is where I need to be able to determine whether or not indeterminate is set on the MatCheckbox referenced by the AbstractControl.

Upvotes: 0

Views: 373

Answers (1)

Eliseo
Eliseo

Reputation: 57939

A checkbox only can be true or false the "indeterminate state" is a new property (in MatCheckbox you use the property [indeterminate] in a normal check-box you can use a directive as in this SO

So the "intermediate state is a property of the "mat-checkbox", not the formControl, so you need use a "template reference variable" and @ViewChild

Another option is that your formControl gets the values 0,1 and -1 and use some like

<mat-checkbox class="example-margin"
                  [checked]="control.value==1"
                  [indeterminate]="control.value==-1"
                  (change)="control.setValue($event.checked)">
      checked
</mat-checkbox>

Really I don't pretty sure what do you want to achieve

Upvotes: 0

Related Questions