0

I have a page where I'm able to do console.log(this.formGroup.controls) and access all the controls on the page. However, when I do console.log(this.formGroup.controls['ctrlName']) I get a null value. I also tried to cast the control as a FormControl like so: console.log(this.formGroup.controls['ctrlName']) as FormControl but still I get null.

How, can I access the individual controls to programatically add validation?

1
  • 1
    could you add all the code with building formGroup ? Commented Jan 13, 2021 at 13:23

1 Answer 1

2

As per Angular docs - since FormGroup inherits after AbstractControl you can use the get method to retrieve control by it's path, e.g.

this.formGroup.get('ctrlName')
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that already as well but same result.
Well, that implies that you provide wrong control name or path. Please, provide a stackblitz reproduction where this method does NOT work and we'll work from there.
ctrlName is a variable or a literal? if is a literal you should access as TotallyNewb say (always you use after create the formGroup)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.