0

I don't know if it's the Angular update, but I notice that it asks to initialize variables, like: A "String" asks to put "", a number to put "0"... But what about when it's a "FormGroup component"? How do I resolve this?

public productForm : FormGroup ; This returns me an error.

I saw that there is an option that puts a "!" at the end of the variable, like this:

public productForm! : FormGroup ;

What's it for?

  • `!` is referred to as **Non-null assertion operator** used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Answers in [this](https://stackoverflow.com/q/38874928/9471852) question may give you more clarity. – Siddhant Feb 01 '22 at 17:06
  • What error are you getting exactly? – HassanMoin Feb 01 '22 at 19:39
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 11 '22 at 02:45

1 Answers1

0

I understand about the exclamation. Then I did some research and solved the issue using this:

this.heroForm = new FormGroup({
    'name': new FormControl(this.hero.name, [
      Validators.required,
      Validators.minLength(4),
      forbiddenNameValidator(/bob/i) // <-- Here's how you pass in the custom validator.
    ]),
    'alterEgo': new FormControl(this.hero.alterEgo),
    'power': new FormControl(this.hero.power, Validators.required)
  });