Solvedangular Property 'controls' does not exist on type 'AbstractControl'
✔️Accepted Answer
@matheo thanks. FormGroup.controls
return type is {[key: string]: AbstractControl}
. inputForm.controls.base
will return AbstractControl
. This is why you got an error.
as a workaround use get
method: inputForm.get('base.monthly').pristine
Other Answers:
I have nested arrays in the same forms, this doesn't work from me . why it should run on dev and after 2 weeks of development we get this error ?
I'd have to say that @DzmitryShylovich 's workaround for AbstractControl doesn't work anymore with 4.0.2. I just think that validation for template on dynamic values is totally wrong even for aot, as there is no way to cast a value in template. Please add casting to template or shut that feature off.
Well, issue is closed, but anyway, as FormGroup
is the key:
buildForm() {
let fb = this.formBuilder;
this.form = fb.group({
animals: fb.group({
cat: '',
dog: '',
other: ''
})
});
}
get animals(): FormGroup {
return this.form.get('animals') as FormGroup;
}
<div class="button-list" formGroupName="animals">
<label class="button" *ngFor="let animal of animals.controls | keys" [ngClass]="{'active': form.value.animals[animal]}">
<input type="checkbox" formControlName="{{animal}}">
<span class="button__text">{{animal}}</span>
</label>
</div>
Angular 4.4.3
UPD.
If you omit as FormGroup
it will work on development and will not throw any errors. But if you run ng build
it will throw you this topic's error message and stops the build.
Yeah @itsnotvalid is right @DzmitryShylovich 's solution doesn't work for me. But I found a way, just reference the formArray in typescript:
formGroup: FormGroup;
formArray: FormArray;
initForm() {
this.formArray = new FormArray([]);
//init form array here - push FormGroups
//then insert form array
this.formBuilder.group({
forms: formArray;
});
}
Then just use *ngFor="let form of formArray.controls
this way you don't need casting anymore
I'm submitting a
Current behavior
As there's no clean access to the
FormGroup
' controls, currently there's ang build -prod
error related to types when you try to access the.controls
property, andAbstractControl
does not have that property:https://github.com/angular/angular/blob/4.0.0/packages/forms/src/model.ts#L834
where I have a form with subgroups like:
and perform template-driven validations per row like:
Expected behavior
ng build -prod
to compile without Type errors.Minimal reproduction of the problem with instructions
References to this issue are in this ticket: #10192 (comment)
Project built with angular/cli 4.0.0 but packages updated today to 4.0.1